Program to Find Sum of Geometric Progression
In this article, we will discuss a C++ program to find the sum of geometric progression.
What is Geometric Progression?
A geometric progression is a particular sequence and a fundamental idea in mathematics. It is frequently shortened to "GP". Each term in a geometric progression is produced by multiplying the one before it by a fixed factor. The initial term (a) is the first term in the sequence, and this constant factor is called the common ratio (r).
Common Ratio (r):
The relationship between successive terms in a geometric progression is determined by the common ratio, or 'r', a fixed number. Any term's ratio to its preceding term is what it is. Mathematically, any term that follows "a_n" and is the first term in the GP, "a", can be written as follows:
a * r^(n-1) = a_n
In this case, 'n' represents the term's position in the sequence, with 1 denoting the first term. A key factor in characterizing the progression is the common ratio, or "r", which can be either positive or negative.
Initial Term (a):
A geometric progression's initial term, commonly represented by the letter "a", is the sequence's beginning. It establishes the framework for the whole process. We can consider 'a' the starting point from which all other terms originate. It is the sequence's value when 'n' equals 1.
Comprehension of these two fundamental elements, the common ratio 'r' and the first term 'a', is necessary when dealing with geometric progressions and figuring out different properties, like the series sum, the nth term, or the number of terms required to get a given value.
The formula for Sum of a Geometric Progression:
The sum of the first 'n' terms of the series, or 'S_n,' in a geometric progression (GP), can be computed using the following formula:
S_n = a * (1 - r^n) / (1 - r)
Below is an explanation of the formula and its elements:
S_n: It represents the geometric progression's first 'n' terms. We wish to compute that value.
a: It is the first term of the GP, and also referred to as the initial term.
r: The common ratio, which establishes the progression as a constant. To get each term, multiply the preceding term by 'r'.
n: The quantity of terms we wish to calculate the sum for. 'n' indicates the number of terms we wish to total.
The formula itself comes from geometric progression properties. It computes the sum by considering the first 'n' terms and using the first term, 'a'. The formula accounts for how the common ratio 'r' influences the terms' generation.
How the Formula Works:
- We commence with the initial term, 'a'.
- The formula determines the value of 'a' for each term up to 'n' using the common ratio 'r'. It is accomplished by raising 'r' to the power of 'n-1', because the generation of each new term comes from multiplying the preceding term by 'r'.
- After that, the sum of the first 'n' terms is determined by the formula using this computed value for 'a'.
- The formula is made to work in various situations, including when 'r' equals 1. It is made possible by the denominator factor (1 - r). In these situations, the formula reduces to 'n * a,' which, when 'r' is 1, is the sum of 'n' identical terms.
Example:
A complete C++ program determines a geometric progression series' sum. This program asks the user for the first term ('a'), common ratio ('r'), and number of terms ('n'). It then uses the formula we previously covered to compute the sum.
#include <iostream> #include <cmath> using namespace std; int main() { double a, r; int n; // Input: First term (a), Common ratio (r), and Number of terms (n) cout << "Enter the first term (a): "; cin >> a; cout << "Enter the common ratio (r): "; cin >> r; cout << "Enter the number of terms (n): "; cin >> n; // Check if the common ratio is 1 to avoid division by zero if (r == 1) { // Handle the special case when r is 1 double sum = n * a; cout << "Sum of the first " << n << " terms: " << sum << endl; } else { // Calculate the sum using the GP formula: S_n = a * (1 - r^n) / (1 - r) double sum = a * (1 - pow(r, n)) / (1 - r); cout << "Sum of the first " << n << " terms: " << sum << endl; } return 0; }
Output: