- 2072 Kartik · 4+4 marks
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
- Unconditional GO TO:
GO TO 100always jumps to statement 100. - Computed GO TO:
GO TO (10, 20, 30), Kjumps to the K-th label in the list (1st label if K = 1, etc.). - 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)
Related questions
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- Sum of two matrices in FORTRAN · Computer Programming, chapter 11
- Series sum 1/1¹+1/2²+… in FORTRAN · Computer Programming, chapter 11
- nth Fibonacci term prime check · Computer Programming, chapter 6
- Structure of a FORTRAN program · Computer Programming, chapter 11 · asked 5×
- Sum of series Σ1/n² · Computer Programming, chapter 5
- C program: sum of two 8-bit BCD numbers · Embedded System, chapter 9
- Series sum using Z-transform · Mathematics III, chapter 3
- Series programming of a digital controller · Digital Control System, chapter 4 · asked 4×
- Series programming of a third-order transfer function · Digital Control System, chapter 4 · asked 2×