You are on page 1of 1

PROGRAM guessing_game

IMPLICIT NONE
REAL :: answer
INTEGER :: the_answer, counter, the_guess
CALL init_random_seed
CALL RANDOM_NUMBER(answer)
the_answer=INT(1+100*answer)
Print*, 'You have 7 tries to guess a number between 1 and 100, please input a nu
mber.'
DO counter= 1, 7
READ*, the_guess
IF (the_guess .lt. the_answer) THEN
PRINT*, the_guess, 'is too low, try again!'
END IF
IF (the_guess .gt. the_answer) THEN
PRINT*, the_guess, 'is too high, try again!'
END IF
IF (the_guess .EQ. the_answer) THEN
PRINT*, 'Congratulations,', the_guess, 'is correct. You won in', counte
r, 'tries!'
STOP
ELSE IF (counter .EQ. 7) THEN
Print *, 'Sorry, you are out of guesses. The correct number was', the_a
nswer, 'better luck next time!'
END IF

END DO
END PROGRAM guessing_game
SUBROUTINE init_random_seed()
implicit none
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))
CALL SYSTEM_CLOCK(COUNT=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
DEALLOCATE(seed)
END SUBROUTINE init_random_seed

You might also like