Skip to main content

Largest of n numbers in FORTRAN

Short answer

Read n and first number as initial largest. Then read remaining n-1 numbers; if a number exceeds current largest, update largest. Output largest. Example: n=5, numbers 12, 45, 7, 89, 23 gives largest 89.

Write a FORTRAN program to read n numbers and display largest number among them.

Method. Read n and the first number, and take it as the largest, BIG. Read each of the remaining n − 1 numbers and replace BIG whenever the new number is larger. No array is needed.

      PROGRAM LARGE
      INTEGER N, I
      REAL X, BIG
      PRINT *, 'HOW MANY NUMBERS?'
      READ *, N
      PRINT *, 'ENTER THE NUMBERS'
      READ *, BIG
      DO 10 I = 2, N
         READ *, X
         IF (X .GT. BIG) BIG = X
   10 CONTINUE
      PRINT *, 'THE LARGEST NUMBER IS ', BIG
      STOP
      END

Sample: n = 5 and the numbers 12, 45, 7, 89, 23 give THE LARGEST NUMBER IS 89.0 (the number of decimal places shown by list-directed output depends on the compiler).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →