! ! This program written by Clint Whaley for CS 1073. It is an optional ! programming exercise given on 9/29/09. ! PROGRAM series ! ! This program prints out the Nth entry for the series: ! S(i) = 100*S(i-2) + S(i-1) ! The user is prompted for N and the seed values S(0) and S(1) ! IMPLICIT NONE INTEGER :: i, N DOUBLE PRECISION :: S0, S1, SN ! ! Prompt user for series number and seed values. ! PRINT *, 'Please enter the sequence value (>=0) you wish to compute:' READ *, N PRINT *, 'Please enter S0' READ *, S0 PRINT *, 'Please enter S1' READ *, S1 ! ! Do some basic error checking on numbers ! IF (N .LT. 0) THEN PRINT *, 'Negative N not allowed!' STOP ELSE IF (N .EQ. 0) THEN PRINT *, S0 STOP ELSE IF (N .EQ. 1) THEN PRINT *, S1 STOP ! ! As an optimization, don't use a loop if both answers are 0: all #s will be 0! ! ELSE IF (S0.EQ.0.0 .AND. S1 .EQ.0) THEN PRINT *, 0.0 STOP END IF ! ! Find the Nth entry in the sequence ! DO i = 2, N SN = 100.0*S0 + S1 S0 = S1 S1 = SN END DO PRINT *, 'The ', N, 'th number in the sequence is:', SN END PROGRAM series