C++ program to generate sum of series 1!+2!+3!+--------------n!

C++ program to generate sum of series 1!+2!+3!+--------------n!

Given a number n , write a program to generate a sum of series 1!+2!+3!+--------------n! . For generating this first we have to know that how to find the factorial of a number .

Program to find the factorial of a number  : -  Click here

Sample input

`3`

Sample output

`Sum of the series is: 9`

Explanation:

`= 1! + 2! + 3!`
`=  1+2+6`
`=  9`

C++ program to generate sum of series 1!+2!+3!+--------------n!

The objective of the code is to generate sum of series 1!+2!+3!+--------------n!  .

Code(C++):-

#include<bits/stdc++.h>
using namespace std;

// function for finding factorial
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
int main()
{
int n,sum=0,i;
cout<<"Enter a number\n";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+fact(i);
}
cout<<"Sum of the series is: "<<sum;
return 0;
}

Output:-

Enter a number
3
Sum of the series is: 9

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-