Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | C ++ Program to compute sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! ; Method to find the factorial of a number ; Method to compute the sum ; Iterate the loop till n and compute the formula ; Driver code ; Get x and n ; Print output
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int fact ( int n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } double sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) { total = total + ( pow ( x , i ) / fact ( i + 1 ) ) ; } return total ; } int main ( ) { int x = 5 , n = 4 ; cout << " Sum ▁ is : ▁ " << sum ( x , n ) ; return 0 ; }