88.4k views
4 votes
Write a program in C to:

a) Calculate Sn=1+1.2+1.2.3+…+1.2.3…n , n>0

User Lrayh
by
6.2k points

1 Answer

6 votes

Answer:

// Program in C to calculate sum of 1+1.2+.....+1.2.3...n

// include header

#include <stdio.h>

// main function

int main()

{

// variable Declaration and initialization

int n,a,prod=1,sum=0;

// ask to enter the value of n

printf("enter the value of n:");

// read the value of n

scanf("%d",&n);

for(a=1;a<=n;a++)

{

// calculate product

prod=prod*a;

// calculate sum of the series

sum=sum+prod;

}

// print the sum

printf("sum of the series is:%d",sum);

return 0;

}

Step-by-step explanation:

Read the value of n from user.initialize variables "prod=1" and

"sum=0".Iterate over a for loop to calculate product term as "prod=prod*a".Here prod will have product of all till "a-1" for each "a".Then "a" is multiplied with "prod" and then add to sum.After the loop ends, "sum" will have the total of all the terms.

Output:

enter the value of n:5

sum of the series is:153

User Greg Haskins
by
6.1k points