Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
You have been given a series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!, find out the sum of the series till nth term.
Examples :
Input :n = 5 Output : 2.70833 Input :n = 7 Output : 2.71806
A simple solution is to one by one computer terms. For every term, find its corresponding factorial value.
An efficient solution is to do factorial computations in same loop.
CPP
// CPP program to print // the sum of series #include<bits/stdc++.h> using namespace std; // function to calculate // sum of given series double sumOfSeries(double num) { double res = 0, fact = 1; for (int i = 1; i <= num; i++) { // fact variable store // factorial of the i fact = fact * i; res = res + (i / fact); } return(res); } // Driver Code int main() { double n = 5; cout << "Sum: " << sumOfSeries(n); return 0; } |
chevron_right
filter_none
Java
// Java program to print // the sum of series import java.io.*; import java.lang.*; class GFG { public static double sumOfSeries(double num) { double res = 0, fact = 1; for (int i = 1; i <= num; i++) { // fact variable store // factorial of the i fact = fact * i; res = res + (i / fact); } return(res); } // Driver Code public static void main (String[] args) { double n = 5; System.out.println("Sum: " + sumOfSeries(n)); } } // This code is contributed by // Mohit Gupta_OMG <(0_o)> |
chevron_right
filter_none
Python3
# Python code to find smallest K-digit # number divisible by X def sumOfSeries(num): # Computing MAX res = 0 fact = 1 for i in range(1, num+1): fact *= i res = res + (i/ fact) return res n = 5print("Sum: ", sumOfSeries(n)) # Code contributed by # Mohit Gupta_OMG <(0_o)> |
chevron_right
filter_none
C#
// C# program to print the sum of series using System; class GFG { public static float sumOfSeries(double num) { float res = 0, fact = 1; for (int i = 1; i <= num; i++) { // fact variable store // factorial of the i fact = fact * i; res = res + (i / fact); } return(res); } // Driver Code public static void Main () { double n = 5; Console.Write("Sum: " + sumOfSeries(n)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to print // the sum of series // Function to calculate // sum of given series function sumOfSeries($num) { $res = 0; $fact = 1; for ($i = 1; $i <= $num; $i++) { // fact variable store // factorial of the i $fact = $fact * $i; $res = $res + ($i / $fact); } return ($res); } // Driver Code $n = 5; echo("Sum: " . sumOfSeries($n)); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output:
Sum: 2.70833
Time Complexity: O(n)
This article is contributed by R_Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find Nth term in the given Series
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2
- Program to find the Nth term of the series 0, 5, 18, 39, 67, 105, 150, 203, ...
- Program to find the Nth term of series 5, 12, 21, 32, 45......
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....
- Program to find N-th term of series 1, 2, 11, 12, 21….
- Program to find Nth term of series 1, 6, 17, 34, 56, 86, 121, 162, .......
Improved By : jit_t



