Skip to main content

Greatest and smallest of ten numbers

Short answer

Read ten numbers into array. Set big and small to first element. Loop from 2 to 10: update big if element is larger, small if smaller. Print greatest and smallest. Example: input 4 9 -2 15 7 3 22 -8 6 11 gives greatest 22.0, smallest -8.0.

Write FORTRAN program to display greatest and smallest number from list ten elements.

Method. The ten numbers are read into an array X. BIG and SMALL both start with X(1). A loop from 2 to 10 compares each element: it updates BIG if the element is larger and SMALL if it is smaller.

      PROGRAM MAXMIN
      REAL X(10), BIG, SMALL
      INTEGER I
      PRINT *, 'ENTER TEN NUMBERS'
      READ *, (X(I), I = 1, 10)
      BIG = X(1)
      SMALL = X(1)
      DO 10 I = 2, 10
         IF (X(I) .GT. BIG) BIG = X(I)
         IF (X(I) .LT. SMALL) SMALL = X(I)
   10 CONTINUE
      PRINT *, 'GREATEST NUMBER = ', BIG
      PRINT *, 'SMALLEST NUMBER = ', SMALL
      STOP
      END

Sample: the numbers 4 9 -2 15 7 3 22 -8 6 11 give greatest 22.0 and smallest -8.0.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →