# Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n

Given an integer X, the task is to print the series and find the sum of the series Examples :

Input: X = 2, N = 5
Output: Sum = 31
1 2 4 8 16

Input: X = 1, N = 10
Output: Sum = 10
1 1 1 1 1 1 1 1 1 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to traverse over the series and compute the sum of the N terms of the series. The Nth term of the series can be computed as:

Nth Term = (N-1)th Term * X

Below is the implementation of the above approach:

## C++

 `// C++ implementatio to find ` `// sum of series of ` `// 1 + x^2 + x^3 + ....+ x^n ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find the sum of ` `// the series and print N terms ` `// of the given series ` `double` `sum(``int` `x, ``int` `n) ` `{ ` `    ``double` `i, total = 1.0, multi = x; ` ` `  `    ``// First Term ` `    ``cout << total << ``" "``; ` ` `  `    ``// Loop to print the N terms ` `    ``// of the series and find their sum ` `    ``for` `(i = 1; i < n; i++) { ` ` `  `        ``total = total + multi; ` `        ``cout << multi << ``" "``; ` `        ``multi = multi * x; ` `    ``} ` ` `  `    ``cout << ``"\n"``; ` `    ``return` `total; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `x = 2; ` `    ``int` `n = 5; ` `    ``cout << fixed ` `         ``<< setprecision(2) ` `         ``<< sum(x, n); ` `    ``return` `0; ` `} `

## C

 `// C implemenation to find the sum ` `// of series 1 + x^2 + x^3 + ....+ x^n ` ` `  `#include ` `#include ` ` `  `// Function to print the sum ` `// of the series ` `double` `sum(``int` `x, ``int` `n) ` `{ ` `    ``double` `i, total = 1.0, multi = x; ` ` `  `    ``// First Term of series ` `    ``printf``(``"1 "``); ` ` `  `    ``// Loop to find the N ` `    ``// terms of the series ` `    ``for` `(i = 1; i < n; i++) { ` ` `  `        ``total = total + multi; ` `        ``printf``(``"%.1f "``, multi); ` `        ``multi = multi * x; ` `    ``} ` `    ``printf``(``"\n"``); ` `    ``return` `total; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `x = 2; ` `    ``int` `n = 5; ` `    ``printf``(``"%.2f"``, sum(x, n)); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to find ` `// the sum of series ` `// 1 + x^2 + x^3 + ....+ x^n ` ` `  `class` `GFG { ` ` `  `    ``// Java code to print the sum ` `    ``// of the given series ` `    ``static` `double` `sum(``int` `x, ``int` `n) ` `    ``{ ` `        ``double` `i, total = ``1.0``, multi = x; ` ` `  `        ``// First Term ` `        ``System.out.print(``"1 "``); ` ` `  `        ``// Loop to print the N terms ` `        ``// of the series and compute ` `        ``// their sum ` `        ``for` `(i = ``1``; i < n; i++) { ` `            ``total = total + multi; ` `            ``System.out.print(multi); ` `            ``System.out.print(``" "``); ` `            ``multi = multi * x; ` `        ``} ` ` `  `        ``System.out.println(); ` `        ``return` `total; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `x = ``2``; ` `        ``int` `n = ``5``; ` `        ``System.out.printf( ` `            ``"%.2f"``, sum(x, n)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find sum of  ` `# series of 1 + x^2 + x^3 + ....+ x^n  ` ` `  `# Function to find the sum of  ` `# the series and print N terms  ` `# of the given series  ` `def` `sum``(x, n):  ` `     `  `    ``total ``=` `1.0` `    ``multi ``=` `x  ` ` `  `    ``# First Term  ` `    ``print``(``1``, end ``=` `" "``)  ` ` `  `    ``# Loop to print the N terms  ` `    ``# of the series and find their sum  ` `    ``for` `i ``in` `range``(``1``, n):  ` `         `  `        ``total ``=` `total ``+` `multi  ` `        ``print``(``'%.1f'` `%` `multi, end ``=` `" "``)  ` `        ``multi ``=` `multi ``*` `x  ` `         `  `    ``print``(``'\n'``) ` `    ``return` `total;  ` ` `  `# Driver code  ` `x ``=` `2` `n ``=` `5` `print``(``'%.2f'` `%` `sum``(x, n)) ` ` `  `# This code is contributed by Pratik Basu  `

## C#

 `// C# implementation to find ` `// the sum of series ` `// 1 + x^2 + x^3 + ....+ x^n ` `using` `System; ` `class` `GFG{ ` ` `  `// C# code to print the sum ` `// of the given series ` `static` `double` `sum(``int` `x, ``int` `n) ` `{ ` `    ``double` `i, total = 1.0, multi = x; ` ` `  `    ``// First Term ` `    ``Console.Write(``"1 "``); ` ` `  `    ``// Loop to print the N terms ` `    ``// of the series and compute ` `    ``// their sum ` `    ``for` `(i = 1; i < n; i++) ` `    ``{ ` `        ``total = total + multi; ` `        ``Console.Write(multi); ` `        ``Console.Write(``" "``); ` `        ``multi = multi * x; ` `    ``} ` `    ``Console.WriteLine(); ` `    ``return` `total; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `x = 2; ` `    ``int` `n = 5; ` `    ``Console.Write(``"{0:F2}"``, sum(x, n)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```1 2.0 4.0 8.0 16.0
31.00
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : PratikBasu, Rajput-Ji