Computer Science, asked by hparty383, 1 month ago

3. Write a Program in C to find the sum of the series: (Calculate the factorials using function) (d) S= 1! + 2! + 3! + 4! + . . .​

Answers

Answered by anindyaadhikari13
7

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language C.

#include <stdio.h>

int fact(int n){

   if(n<=1)

       return 1;

   return n*fact(n-1);

}

int main() {

   int n,i,s=0;

   printf("Enter limit - ");

   scanf("%d",&n);

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

       s+=fact(i);

   printf("Sum of the series upto n terms is: %d",s);

   return 0;

}

\textsf{\large{\underline{Explanation}:}}

  • The function fact() calculates the factorial of a number using recursion.
  • Now, inside main() method, we have asked the user to enter the limit.
  • The function fact() is called inside the loop so as to calculate the sum of the series.

\textsf{\large{\underline{Sample I/O}:}}

#1

Enter limit - 5

Sum of the series upto n terms is: 153

#2

Enter limit - 4

Sum of the series upto n terms is: 33

Attachments:
Similar questions