# Program to calculate sum of an Infinite Arithmetic-Geometric Sequence

Given three integers **A**, **D**, and **R** representing the first term, common difference, and common ratio of an infinite Arithmetic-Geometric Progression, the task is to find the sum of the given infinite Arithmetic-Geometric Progression such that the absolute value of **R** is always less than **1**.

**Examples:**

Input:A = 0, D = 1, R = 0.5Output:0.666667

Input:A = 2, D = 3, R = -0.3Output:0.549451

**Approach:** An arithmetic-geometric sequence is the result of the term-by-term multiplication of the geometric progression series with the corresponding terms of an arithmetic progression series. The series is given by:

a, (a + d) * r, (a + 2 * d) * r

^{2}, (a + 3 * d) * r^{3}, …, [a + (N − 1) * d] * r^{(N − 1)}.

The **N ^{th} term** of the Arithmetic-Geometric Progression is given by:

=>

The sum of the Arithmetic-Geometric Progression is given by:

=>

where, |r| < 1.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the sum of the` `// infinite AGP` `void` `sumOfInfiniteAGP(` `double` `a, ` `double` `d,` ` ` `double` `r)` `{` ` ` `// Stores the sum of infinite AGP` ` ` `double` `ans = a / (1 - r)` ` ` `+ (d * r) / (1 - r * r);` ` ` `// Print the required sum` ` ` `cout << ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `double` `a = 0, d = 1, r = 0.5;` ` ` `sumOfInfiniteAGP(a, d, r);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG{` `// Function to find the sum of the` `// infinite AGP` `static` `void` `sumOfInfiniteAGP(` `double` `a, ` `double` `d,` ` ` `double` `r)` `{` ` ` ` ` `// Stores the sum of infinite AGP` ` ` `double` `ans = a / (` `1` `- r) +` ` ` `(d * r) / (` `1` `- r * r);` ` ` `// Print the required sum` ` ` `System.out.print(ans);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `double` `a = ` `0` `, d = ` `1` `, r = ` `0.5` `;` ` ` ` ` `sumOfInfiniteAGP(a, d, r);` `}` `}` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python3 program for the above approach` `# Function to find the sum of the` `# infinite AGP` `def` `sumOfInfiniteAGP(a, d, r):` ` ` ` ` `# Stores the sum of infinite AGP` ` ` `ans ` `=` `a ` `/` `(` `1` `-` `r) ` `+` `(d ` `*` `r) ` `/` `(` `1` `-` `r ` `*` `r);` ` ` `# Print the required sum` ` ` `print` `(` `round` `(ans,` `6` `))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `a, d, r ` `=` `0` `, ` `1` `, ` `0.5` ` ` `sumOfInfiniteAGP(a, d, r)` `# This code is contributed by mohit kumar 29.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG` `{` ` ` ` ` `// Function to find the sum of the` ` ` `// infinite AGP` ` ` `static` `void` `sumOfInfiniteAGP(` `double` `a, ` `double` `d,` ` ` `double` `r)` ` ` `{` ` ` ` ` `// Stores the sum of infinite AGP` ` ` `double` `ans = a / (1 - r) + (d * r) / (1 - r * r);` ` ` `// Print the required sum` ` ` `Console.Write(ans);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `double` `a = 0, d = 1, r = 0.5;` ` ` `sumOfInfiniteAGP(a, d, r);` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// Javascript program for the above approach` ` ` `// Function to find the sum of the` ` ` `// infinite AGP` ` ` `function` `sumOfInfiniteAGP(a, d, r) {` ` ` `// Stores the sum of infinite AGP` ` ` `let ans = a / (1 - r) +` ` ` `(d * r) / (1 - r * r);` ` ` `// Print the required sum` ` ` `document.write(ans)` ` ` `}` ` ` `// Driver Code` ` ` `let a = 0, d = 1, r = 0.5;` ` ` `sumOfInfiniteAGP(a, d, r);` ` ` `// This code is contributed by Hritik` ` ` `</script>` |

**Output**

0.666667

**Time Complexity:** O(1)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**