Skip to main content

RANGUESS.F95

	PROGRAM RANGUESS
!
!  Purpose: To enable the user to play at guessing a number.
!
	IMPLICIT NONE
	REAL:: X		! Holds random number
	REAL:: G,TOL		! Holds guess number and tolerance
!
!  Initialize the sequence at random and place a random number in X:
	CALL RANDOM_SEED
 	CALL RANDOM_NUMBER(X)
!  Mulitply X by 10, to have a number between 0 and 10.
	X = 10.*X
!
!  Ask for a guess. Exit if the guess is negative or the guess is within
!  a distance TOL from the random number.
	TOL=0.2
	DO
	   WRITE(*,*)'Enter a guess, between 0 and 10, or a negative number',&
	                 ' to exit.'
!  Note that the previous WRITE statement is too long to fit on a line. We
!  used a continuation line.
	   READ(*,*)G
	   IF(G < 0.)EXIT
	   IF(G > 10.)THEN
	      WRITE(*,*)'Your guess should not exceed 10.'
	      CYCLE
	   ENDIF
	   IF(ABS(G-X) <= TOL)THEN
	      WRITE(*,*)'Good guess ! The random number is ',X
	      EXIT
	   ELSE
!  At this stage, G cannot be equal to X...
	      IF(G < X)THEN
	         WRITE(*,*)'Too low !'
	      ELSE
	         WRITE(*,*)'Too high !'
	      ENDIF
	   ENDIF
	END DO
	
	STOP
	END PROGRAM RANGUESS