# Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!

Given two values ‘a’ and ‘n’, find sum of series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!.

**Examples :**

Input : a = 2 and n = 5 Output : 6.26667 We get result by adding 2^1/1! + 2^2/2! + 2^3/3! + 2^4/4! + 2^5/5! = 2/1 + 4/2 + 8/6 + 16/24 + 32/120 = 6.26667

A simple solution is to one by one compute values of individual terms and keep adding them to result.

We can find solution with only one loop. The idea is to just use previous values and multiply by (a/i) where i is the no of term which we need to find.

for finding 1st term:- a/1 for finding 2nd term:- (1st term) * a/2 for finding 3rd term:- (2nd term) * a/3 . . . for finding nth term:- ((n-1)th term) * a/n

Illustration:

Input: a = 2 and n = 5 By multiplying Each term by 2/i 1st term :- 2/1 = 2 2nd term :- (1st term) * 2/2 =(2)*1 = 2 3rd term :- (2nd term) * 2/3 = 4/3 4th term :- (3rd term) * 2/4 = 2/3 5th term :- (4th term) * 2/5 = 4/15 => 2 + 2 + 4/3 + 2/3 + 4/15 Output: sum = 6.26667

**Complexity:O(n)**

## CPP

`/*CPP program to print the sum of series */` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/*f unction to calculate sum of given series*/` `double` `sumOfSeries(` `double` `a,` `double` `num) ` `{ ` ` ` `double` `res = 0,prev=1; ` ` ` `for` `(` `int` `i = 1; i <= num; i++) ` ` ` `{ ` ` ` `/*multiply (a/i) to previous term*/` ` ` `prev *= (a/i); ` ` ` ` ` `/*store result in res*/` ` ` `res = res + prev; ` ` ` `} ` ` ` `return` `(res); ` `} ` ` ` `/* Driver Function */` `int` `main() ` `{ ` ` ` `double` `n = 5, a=2; ` ` ` `cout << sumOfSeries(a,n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the ` `// sum of series ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `double` `n = ` `5` `, a = ` `2` `; ` ` ` `System.out.println(sumOfSeries(a, n)); ` ` ` `} ` ` ` ` ` `// function to calculate sum of given series ` ` ` `static` `double` `sumOfSeries(` `double` `a,` `double` `n) ` ` ` `{ ` ` ` `double` `res = ` `0` `, prev = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `// multiply (a/i) to previous term ` ` ` `prev *= (a / i); ` ` ` ` ` `// store result in res ` ` ` `res = res + prev; ` ` ` `} ` ` ` `return` `(res); ` ` ` `} ` `} ` ` ` `// This code is Contributed by Azkia Anam. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to print ` `# the sum of series. ` `# function to calculate ` `# sum of given series. ` ` ` `from` `__future__ ` `import` `division ` ` ` `def` `sumOfSeries(a,num): ` ` ` `res ` `=` `0` ` ` `prev` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n` `+` `1` `): ` ` ` ` ` `# multiply (a/i) to ` ` ` `# previous term ` ` ` `prev ` `*` `=` `(a` `/` `i) ` ` ` ` ` `# store result in res ` ` ` `res ` `=` `res ` `+` `prev ` ` ` `return` `res ` ` ` `# Driver code ` `n ` `=` `5` `a ` `=` `2` `print` `(` `round` `(sumOfSeries(a,n),` `4` `)) ` ` ` `# This Code is Contributed ` `# by Azkia Anam. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the ` `// sum of series ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `double` `n = 5, a = 2; ` ` ` `Console.WriteLine(sumOfSeries(a, n)); ` ` ` `} ` ` ` ` ` `// Function to calculate sum of given series ` ` ` `static` `float` `sumOfSeries(` `double` `a, ` `double` `n) ` ` ` `{ ` ` ` `double` `res = 0, prev = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `// multiply (a/i) to previous term ` ` ` `prev *= (a / i); ` ` ` ` ` `// store result in res ` ` ` `res = res + prev; ` ` ` `} ` ` ` `return` `(` `float` `)(res); ` ` ` `} ` `} ` ` ` `// 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(` `$a` `, ` `$num` `) ` `{ ` ` ` `$res` `= 0; ` `$prev` `= 1; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$num` `; ` `$i` `++) ` ` ` `{ ` ` ` `// multiply (a/i) to ` ` ` `// previous term ` ` ` `$prev` `*= (` `$a` `/ ` `$i` `); ` ` ` ` ` `// store result in res ` ` ` `$res` `= ` `$res` `+ ` `$prev` `; ` ` ` `} ` ` ` `return` `(` `$res` `); ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$a` `= 2; ` `echo` `(sumOfSeries(` `$a` `, ` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

6.26667

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 the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- 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 series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find Sum of the series 1*3 + 3*5 + ....
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find N-th term of series 3, 5, 33, 35, 53....
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- 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 the N-th term of series 3, 5, 33, 35, 53…. | Set-2