Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Find Sum of GP Series

    C Programming Examples


    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 or a 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 is a, the common ratio is r, and the number of terms is n.

    C Program to Find the Sum of a GP Series Using a Formula

    To use the above formula in C, you need to include the math.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 the sum.

    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: 2
    Enter the common ratio: 3
    Enter the number of terms: 5
    The sum of the GP series is: 242.00

    Explanation:

    In the above program, we declare four variables: a, r, n, and sum. 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 the printf() 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.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.