- 2067 Asoj · 7 marks
Series (2*3/5)+(4*5/7)+… with functions
Short answer
The i-th term is (2i)*(2i+1)/(2i+3) computed in float. sumSeries loops i=1 to n, adding term(i). For n=3, the sum is 8.724.
Write code in C to calculate the sum of following series up to n terms specified by the user where n is passed to the function that calculates the sum. Your program should have more than two functions. (23/5) + (45/7) + (6*7/9) + ........
Analysis. Looking at the series (2·3)/5 + (4·5)/7 + (6·7)/9 + …, the i-th term is
term(i) = (2i)(2i + 1) / (2i + 3)
Check: i = 1 gives 2·3/5, i = 2 gives 4·5/7, i = 3 gives 6·7/9. The sum is term(1) + … + term(n).
The program uses three functions: main() reads n, sumSeries(n) receives n and adds the terms, and term(i) returns one term. The terms are computed in float so that the division is not truncated as integer division.
#include <stdio.h>
/* i-th term = (2i)(2i+1)/(2i+3) */
float term(int i)
{
return (float)(2 * i) * (2 * i + 1) / (2 * i + 3);
}
float sumSeries(int n)
{
float s = 0;
for (int i = 1; i <= n; i++)
s += term(i);
return s;
}
int main(void)
{
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Sum of %d terms = %.3f\n", n, sumSeries(n));
return 0;
}
Sample run (n = 3)
Input: 3
Enter number of terms: Sum of 3 terms = 8.724
Verification: 1.2 + 2.857 + 4.667 = 8.724.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- GOTO types; sum of series program · Computer Programming, chapter 11
- Function definition; sum of two numbers · Computer Programming, chapter 6
- Cosine series up to n terms · Computer Programming, chapter 5
- Complex number sum via functions · Computer Programming, chapter 8
- Sum of series Σ1/n² · Computer Programming, chapter 5
- Sum of two matrices in FORTRAN · Computer Programming, chapter 11
- C program: sum of two 8-bit BCD numbers · Embedded System, chapter 9
- 8085 ALP: transfer data with more than 3 ones · Microprocessor, chapter 2
- T/F: radial more reliable than network · Transmission and Distribution System Design, chapter 5 · asked 2×
- RE: no more than 3 a's · Theory of Computation, chapter 2