Q. 29. Write a FORTRAN program that goes on reading values for an integer variable N until the value read is zero or negative. For each positive value of N read, the program tests whether N is a prime number or not. Also it should print appropriate message. (Dec. 2001)

Solution.
INTEGER N, I, J, K, L
WRITE (*, 10)
10     FORMAT(1X, 'ENTER VALUE OF N = ',\)
READ (*, 11) N
11     FORMAT(I3)
100    IF (N .LE. 0) THEN GOTO 200
K = 0
I = 2
20    IF (I .GT. N/2) THEN GOTO 40
IF (MOD (N, I) .NE. 0) THEN
WRITE (*, 30) N
30     FORMAT(1X, 'I3 IS NOT A PRIME NUMBER')
GOTO 150
ENDIF
I = I + 1
GOTO 20
40      WRITE (*, 50) N
50     FORMAT(1X, 'I3 IS A PRIME NUMBER')
150      WRITE (*, 55)
55     FORMAT(1X, 'ENTER VALUE OF N = ',\)
READ (*, 60) N
60     FORMAT(I3)
GOTO 100
200      STOP
END

Q. 30. Write a FORTRAN program which finds all prime numbers between 1000 and 9999. (June 2000)

Solution.
INTEGER N, K, M
5     DO 20 N = 1000, 9999
K = 2
M = SQRT (N)
10    IF (MOD (N, K) .EQ. 0) GOTO 5
K = K + 1
IF (K .LE. M) THEN GOTO 10
WRITE (*, 15) N
15     FORMAT(1X, I5, 'IS A PRIME NUMBER')
20     CONTINUE
STOP
END



Main Contents Page

© Universal Teacher Publications