C Program to Print Geometric Progression(GP) Series and it's Sum till N Terms

  • Write a C program to print geometric series till Nth term
  • Write a C program to find sum of geometric series till Nth term

Geometric series is a sequence of terms in which next term is obtained by multiplying common ration to previous term. The (n+1)th term of GP can be calculated as
(n+1)th = nth x R
where R is the common ratio (n+1)th/nth
The formula to calculate Nth term of GP : tn = a x rn-1
where, a is first term of GP and r is the common ratio.

C program to print geometric progression series and it's sum till N terms

In this program, we first take number of terms, first term and common ratio as input from user using scanf function. Then we calculate the geometric series using above formula(by multiplying common ratio to previous term) inside a for loop. We keep on adding the current term's value to sum variable.


/*
* C program to generate Geometric Series and it's sum till Nth term
*/
#include <stdio.h>
#include <stdlib.h>

int main() {
    int first, ratio, terms, value, sum=0, i;

    printf("Enter the number of terms in GP series\n");
    scanf("%d", &terms);

    printf("Enter first term and common ratio of GP series\n");
    scanf("%d %d", &first, &ratio);

    /* print the series and add all elements to sum */
    value = first;
    printf("GP SERIES\n");
    for(i = 0; i < terms; i++) {
        printf("%d ", value);
        sum += value;
        value = value * ratio;
    }

    printf("\nSum of the GP series till %d terms is %d\n", terms, sum);

    getch();
 return 0;
}
Program Output
Enter the number of terms in GP series
6
Enter first term and common ratio of GP series
2 4
GP SERIES
2 4 8 16 32 64
Sum of the GP series till 6 terms is 126

Related Topics
C Program to generate arithmetic progression(AP) series
C Program to generate harmonic progression(HP) series
C program to convert decimal number to hexadecimal number system
C program to convert decimal numbers to binary numbers
C program to multiply two numbers without using arithmetic operators
C program to convert temperature from celsius to fahrenheit
C program to convert binary number to decimal number system
C program to convert octal number to binary number system
C program to make a simple calculator using switch statement
List of all C programs