Skip to main content

GOTO types; sum of series program

Short answer

Unconditional GO TO jumps to one label. Computed GO TO selects a label from a list by integer value. Assigned GO TO uses a label stored via ASSIGN. The series sum program reads n, computes sum = 1 + (1+2) + (1+2+3) + ... up to n terms using a loop: accumulate term T = T + i and add to S. For n=4, sum=20.

Explain different types of goto statements in FORTRAN programming with suitable example. Write a program to read n from user and display the sum of following series till nth terms: 1+(1+2)+(1+2+3)+(1+2+3+4)+...........n

Types of GO TO statements

  1. Unconditional GO TO: GO TO 100 always jumps to statement 100.
  2. Computed GO TO: GO TO (10, 20, 30), K jumps to the K-th label in the list (1st label if K = 1, etc.).
  3. Assigned GO TO: a label is first stored in an integer variable with ASSIGN, then used.
      ASSIGN 40 TO LBL
      GO TO LBL, (40, 50)

(Related: the arithmetic IF IF (X) 10, 20, 30 is also a three-way jump, by the sign of X.)

Example of computed GO TO:

      READ *, K
      GO TO (10, 20), K
      PRINT *, 'OTHER'
      STOP
   10 PRINT *, 'ONE'
      STOP
   20 PRINT *, 'TWO'
      END

Sum of the series 1 + (1+2) + (1+2+3) + … up to n terms

The i-th term is T = 1 + 2 + … + i. Each new term is the previous term plus i, so the program keeps a running term T and adds it to the sum S. The loop is made with an IF and a GO TO.

      PROGRAM SERIES
      INTEGER N, I, T, S
      PRINT *, 'ENTER N'
      READ *, N
      S = 0
      T = 0
      I = 1
   10 IF (I .LE. N) THEN
         T = T + I
         S = S + T
         I = I + 1
         GO TO 10
      END IF
      PRINT *, 'SUM OF THE SERIES = ', S
      STOP
      END

Sample: N = 4 gives 1 + 3 + 6 + 10 = 20 (the formula n(n+1)(n+2)/6 = 4·5·6/6 = 20 agrees).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 11 questions →