Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!

15 Jun, 2022
Given a number x and n, the task is to find the sum of the below series of x till n terms:  
Examples:  
Input: x = 5, n = 2
Output: 7.67
Explanation:
    Sum of first two termed
    Input: x = 5, n = 4Output: 18.08Explanation: 
Approach: Iterate the loop till the nth term, compute the formula in each iteration i.e.  
nth term of the series = 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to compute sum of// 1 + x/2! + x^2/3! +...+x^n/(n+1)! #include <iostream>#include <math.h>using namespace std; // Method to find the factorial of a numberint fact(int n){    if (n == 1)        return 1;     return n * fact(n - 1);} // Method to compute the sumdouble sum(int x, int n){    double i, total = 1.0;     // Iterate the loop till n    // and compute the formula    for (i = 1; i <= n; i++) {        total = total + (pow(x, i) / fact(i + 1));    }     return total;} // Driver codeint main(){     // Get x and n    int x = 5, n = 4;     // Print output    cout << "Sum is: " << sum(x, n);     return 0;}
// Java Program to compute sum of// 1 + x/2! + x^2/3! +...+x^n/(n+1)! public class SumOfSeries {     // Method to find factorial of a number    static int fact(int n)    {        if (n == 1)            return 1;         return n * fact(n - 1);    }     // Method to compute the sum    static double sum(int x, int n)    {        double total = 1.0;         // Iterate the loop till n        // and compute the formula        for (int i = 1; i <= n; i++) {            total = total + (Math.pow(x, i) / fact(i + 1));        }         return total;    }     // Driver Code    public static void main(String[] args)    {         // Get x and n        int x = 5, n = 4;         // Find and print the sum        System.out.print("Sum is: " + sum(x, n));    }}
# Python3 Program to compute sum of# 1 + x / 2 ! + x ^ 2 / 3 ! +...+x ^ n/(n + 1)! # Method to find the factorial of a numberdef fact(n):    if n == 1:        return 1    else:        return n * fact(n - 1) # Method to compute the sumdef sum(x, n):    total = 1.0     # Iterate the loop till n    # and compute the formula    for i in range (1, n + 1, 1):        total = total + (pow(x, i) / fact(i + 1))     return total # Driver codeif __name__== '__main__':         # Get x and n    x = 5    n = 4     # Print output    print ("Sum is: {0:.4f}".format(sum(x, n)))     # This code is contributed by# SURENDRA_GANGWAR
// C# Program to compute sum of// 1 + x/2! + x^2/3! +...+x^n/(n+1)!using System; class SumOfSeries {     // Method to find factorial of a number    static int fact(int n)    {        if (n == 1)            return 1;         return n * fact(n - 1);    }     // Method to compute the sum    static double sum(int x, int n)    {        double total = 1.0;         // Iterate the loop till n        // and compute the formula        for (int i = 1; i <= n; i++) {            total = total + (Math.Pow(x, i) / fact(i + 1));        }         return total;    }     // Driver Code    public static void Main()    {         // Get x and n        int x = 5, n = 4;         // Find and print the sum        Console.WriteLine("Sum is: " + sum(x, n));    }} // This code is contributed// by anuj_67..
<?php// PHP Program to compute sum of// 1 + x/2! + x^2/3! +...+x^n/(n+1)! // Function to find the factorial// of a numberfunction fact($n){    if ($n == 1)        return 1;     return $n * fact($n - 1);} // Function to compute the sumfunction sum($x, $n){    $total = 1.0;     // Iterate the loop till n    // and compute the formula    for ($i = 1; $i <= $n; $i++)    {        $total = $total + (pow($x, $i) /                          fact($i + 1));    }     return $total;} // Driver code // Get x and n$x = 5;$n = 4; // Print outputecho "Sum is: ", sum($x, $n); // This code is contributed by ANKITRAI1?>
<script>// java script  Program to compute sum of// 1 + x/2! + x^2/3! +...+x^n/(n+1)! // Function to find the factorial// of a numberfunction fact(n){    if (n == 1)        return 1;     return n * fact(n - 1);} // Function to compute the sumfunction sum(x, n){    let total = 1.0;     // Iterate the loop till n    // and compute the formula    for (let i = 1; i <= n; i++)    {        total = total + (Math.pow(x, i) /                        fact(i + 1));    }     return total.toFixed(4);} // Driver code // Get x and nlet x = 5;let n = 4; // Print outputdocument.write( "Sum is: "+ sum(x, n)); // This code is contributed by sravan kumar Gottumukkala</script>
Sum is: 18.0833
 
Time Complexity: O(n2)
Auxiliary Space: O(n)
Efficient approach: Time complexity for above algorithm is O() because for each sum iteration factorial is being calculated which is O(n). It can be observed that term of the series can be written as , where . Now we can iterate over to calculate the sum.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function to compute the series sumdouble sum(int x, int n){    double total = 1.0;     // To store the value of S[i-1]    double previous = 1.0;     // Iterate over n to store sum in total    for (int i = 1; i <= n; i++)    {         // Update previous with S[i]        previous = (previous * x) / (i + 1);        total = total + previous;    }    return total;} // Driver codeint main(){    // Get x and n    int x = 5, n = 4;         // Find and print the sum    cout << "Sum is: " << sum(x, n);     return 0;} // This code is contributed by jit_t
// Java implementation of the approach public class GFG {     // Function to compute the series sum    static double sum(int x, int n)    {         double total = 1.0;         // To store the value of S[i-1]        double previous = 1.0;         // Iterate over n to store sum in total        for (int i = 1; i <= n; i++) {             // Update previous with S[i]            previous = (previous * x) / (i + 1);            total = total + previous;        }         return total;    }     // Driver code    public static void main(String[] args)    {         // Get x and n        int x = 5, n = 4;         // Find and print the sum        System.out.print("Sum is: " + sum(x, n));    }}
# Python implementation of the approach # Function to compute the series sumdef sum(x, n):    total = 1.0;     # To store the value of S[i-1]    previous = 1.0;     # Iterate over n to store sum in total    for i in range(1, n + 1):                 # Update previous with S[i]        previous = (previous * x) / (i + 1);        total = total + previous;     return total; # Driver codeif __name__ == '__main__':         # Get x and n    x = 5;    n = 4;     # Find and print the sum    print("Sum is: ", sum(x, n)); # This code is contributed by 29AjayKumar
// C# implementation of the approachusing System; class GFG{     // Function to compute the series sum    public double sum(int x, int n)    {        double total = 1.0;         // To store the value of S[i-1]        double previous = 1.0;         // Iterate over n to store sum in total        for (int i = 1; i <= n; i++)        {             // Update previous with S[i]            previous = ((previous * x) / (i + 1));            total = total + previous;        }         return total;    }} // Driver codeclass geek{    public static void Main()    {        GFG g = new GFG();         // Get x and n        int x = 5, n = 4;         // Find and print the sum        Console.WriteLine("Sum is: " + g.sum(x, n));    }} // This code is contributed by SoM15242
<script>    // Javascript implementation of the approach         // Function to compute the series sum    function sum(x, n)    {        let total = 1.0;          // To store the value of S[i-1]        let previous = 1.0;          // Iterate over n to store sum in total        for (let i = 1; i <= n; i++)        {              // Update previous with S[i]            previous = ((previous * x) / (i + 1));            total = total + previous;        }          return total;    }         // Get x and n    let x = 5, n = 4;     // Find and print the sum    document.write("Sum is: " + sum(x, n));     </script>
Sum is: 18.083333333333336
 
Time Complexity: O(n)Auxiliary Space: O(1) since using constant variables
vt_m
SURENDRA_GANGWAR
ankthon
rahul_gfg
SoumikMondal
jit_t
29AjayKumar
sravankumar8128
divyeshrabadiya07
ankita_saini
surinderdawra388
polymatir3j
series
series-sum
Mathematical
School Programming
Technical Scripter
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java