Consider the following program: #include<stdio.h> main() { int i, inp; float…
2015
Consider the following program:
#include<stdio.h>
main()
{
int i, inp;
float x, term=1, sum=0;
scanf("%d %f", &inp, &x);
for(i=1;i<=inp;i++)
{
term=term*x/i;
sum=sum+term;
}
printf("Result=%f\n", sum);
}
The program computes the sum of which of the following series?
- A.
\(x+x^2/2+x^3/3+x^4/4 + \dots\) - B.
\(x+x^2/2!+x^3/3!+x^4/4! + \dots\) - C.
\(1+x^2/2+x^3/3+x^4/4 + \dots\) - D.
\(1+x^2/2!+x^3/3!+x^4/4! + \dots\)
Attempted by 174 students.
Show answer & explanation
Correct answer: B
Answer: The program computes the sum x + x^2/2! + x^3/3! + ... + x^n/n!, where n = inp.
Initial values: term = 1, sum = 0.
On the first loop iteration (i = 1) the code does term = term * x / i = 1 * x / 1 = x, and then adds term to sum.
Each iteration multiplies the previous term by x and divides by the current i, so the denominator accumulates multiplicatively. By induction, after iteration i the term equals x^i / i! (for example, i = 3 gives x^3/(1*2*3) = x^3/6 = x^3/3!).
Since sum is updated by adding term for i = 1 to inp, the final sum is Σ (x^i / i!) for i = 1..inp.
Note: The initial term value 1 is not added to sum because term is updated before being added, so the series does not include a leading constant 1.