CSC115 ยท 2081 ยท 5 marks
Write a program to display the following series up to 25 terms but do not print the 7th term. 2ร3, 3ร5, 4ร7, 5ร9...
Write a program to display the following series up to 25 terms but do not print the 7th term. 2ร3, 3ร5, 4ร7, 5ร9... [5]
Program to Display Series Up to 25 Terms (Skipping the 7th Term)
Given
- Series: $2\times3,; 3\times5,; 4\times7,; 5\times9,\ldots$
- Number of terms: 25
- Condition: Do not print the 7th term
Solution
Pattern Analysis
| Term ($i$) | Expression | First Factor | Second Factor |
|---|---|---|---|
| 1 | $2\times3$ | 2 | 3 |
| 2 | $3\times5$ | 3 | 5 |
| 3 | $4\times7$ | 4 | 7 |
| 4 | $5\times9$ | 5 | 9 |
- First factor $a = i + 1$
- Second factor $b = 2i + 1$
Verification for $i=4$: $a=5$, $b=9$ โ $5\times9$. Correct.
C Program
#include <stdio.h>
int main()
{
int i, a, b;
printf("Series: 2x3, 3x5, 4x7, 5x9 ... up to 25 terms\n");
printf("(7th term is skipped)\n\n");
for(i = 1; i <= 25; i++)
{
if(i == 7) /* Skip the 7th term */
continue;
a = i + 1; /* First factor */
b = 2 * i + 1; /* Second factor */
printf("Term %d: %d x %d = %d\n", i, a, b, a * b);
}
return 0;
}
Sample Output
Series: 2x3, 3x5, 4x7, 5x9 ... up to 25 terms
(7th term is skipped)
Term 1: 2 x 3 = 6
Term 2: 3 x 5 = 15
Term 3: 4 x 7 = 28
Term 4: 5 x 9 = 45
Term 5: 6 x 11 = 66
Term 6: 7 x 13 = 91
Term 8: 9 x 17 = 153
Term 9: 10 x 19 = 190
...
Term 25: 26 x 51 = 1326
Verification of Skipped Term
7th term would be: $a = 8,; b = 15 \Rightarrow 8\times15 = 120$ - this is skipped by the continue statement.
Key Points
- A
forloop iterates $i = 1$ to $25$ (25 terms). if(i == 7) continue;skips only the 7th term.- Formulas: $a = i+1$, $b = 2i+1$.
The program above uses standard C in place of the non-standard Turbo C constructs (void main(), conio.h, clrscr() and getch()); the logic, formulas and output are identical.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.