This tutorial guides you through writing a C program to find the sum of a Geometric Progression (GP) series. You'll learn two approaches: using a formula and using a loop. The tutorial will also cover the logic and concept behind the program and provide examples and code snippets.
What is Geometric Progression (GP) Series
A Geometric Progression (GP) is a mathematical concept that involves a sequence of numbers following a specific pattern. The pattern is based on multiplying the previous term by a fixed number, known as the common ratio. For example, consider the sequence 2, 6, 18, 54, and so on, which is a geometric progression with a common ratio of 3. GP is commonly found in mathematical and scientific applications, and understanding their properties is essential in solving many problems in these fields.
Methods to Find Sum of GP Series
Two methods are commonly used to find the sum of geometric progressions:using a formula ora loop.
Method 1: Using a Formula
The sum of a GP series can be calculated using the following formula:
S = a(1 - r^n) / (1 - r)Where the first term isa, the common ratio isr, and the number of terms isn.
C Program to Find the Sum of a GP Series Using a Formula
To use the above formula in C, you need to include themath.h header file, which provides the pow() function to calculate the power of a number. You also need to declare and initialize the variables for the first term, the common ratio, and the number of terms, and then use the formula to calculate and print thesum.
The following code snippet shows how to use the formula to find the sum of a GP series in C:
#include <stdio.h>#include <math.h>int main() { float a, r, n, sum; // Variables for first term, ratio, number of terms, sum // Input: first term printf("Enter the first term: "); scanf("%f", &a); // Input: common ratio printf("Enter the common ratio: "); scanf("%f", &r); // Input: number of terms printf("Enter the number of terms: "); scanf("%f", &n); sum = (a * (1 - pow(r, n))) / (1 - r); // Calculate sum of GP printf("The sum of the GP series is: %.2f", sum); // Output sum return 0; // Program end}Program Output:
Enter the first term: 2Enter the common ratio: 3Enter the number of terms: 5The sum of the GP series is: 242.00Explanation:
In the above program, we declare four variables: a,r,n, andsum. Afterwards, we prompt the user to input the first term, common ratio, and number of terms. Using the earlier formula, we calculate the GP series sum and store the result in the sum variable. We utilize theprintf() function to display the GP series sum.
Method 2: Using a Loop
Another way to find the sum of a GP series is to use a loop that iterates over the terms of the series and adds them to a variable that stores the sum.
C Program to Find the Sum of a GP Series Using a Loop
The following code snippet shows how to use a for loop to find the sum of a GP series in C:
#include <stdio.h>int main() { float a, r, n, sum = 0, term; // Variables for first term, ratio, number of terms, sum // Input: first term printf("Enter the first term: "); scanf("%f", &a); // Input: common ratio printf("Enter the common ratio: "); scanf("%f", &r); // Input: number of terms printf("Enter the number of terms: "); scanf("%f", &n); term = a; // Initialize term for (int i = 1; i <= n; i++) { sum += term; // Add term to sum term *= r; // Update term } printf("The sum of the GP series is: %.2f", sum); // Output sum return 0; // Program end}Conclusion
This tutorial taught you how to calculate the sum of a geometric progression series using two different methods in C programming, enhancing your understanding of series and sequences.