Java Program to Find Sum of Geometric Progression

In the previous article, we have discussed about Java Program to Find Sum of Arithmetic Progression

In this article we are going to see find sum of Geometric Progression using Java programming language, Geometric Sequence Program In Java Using For Loop, to implement A Program To Display The Geometric Sequence In Java, Geometric Progression Program In Java, Program To Display Geometric Sequence In Java, Geometric Progression In Java, Sum Of Geometric Progression Program In Java, Geometric Progression Java Program, Geometric Sum Using Recursion, Geometric Sequence Java Program, Geometric Sum In Java are discussed below.

Java Program to Find Sum of Geometric Progression

The formula to calculate the sum of arithmetic progression is

If r > 0:
s_n= (a(r^n-1))/(r-1)

If r < 0:
s_n= (a(1-r^n))/(1-r)

where,

  • a = first number the GP
  • n = number of terms in the GP
  • r = common ratio of the elements

Example:

In an GP if the first term i.e., a = 1, total number of terms i.e., n = 5 and the common ratio i.e. r = 2, then
s_n= (a(r^n-1))/(r-1)
= (1×(2^5-1))/(2-1)
= (32-1)/1
= 31

We can also obtain the nth term of an GP series by using the formula

Tn = a + (n – 1) d

Let’s see different ways to find sum of Geometric Progression .

Method-1: Java Program to Find Sum of Geometric Progression By Using User Input Value and Using the Formula

Approach:

  • Take the user input for the first term, common difference, and the number of terms.
  • Use the formula to get the sum of the series and print the result.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first term of the series: ");
        int a = sc.nextInt();
        System.out.print("Enter the common ratio: ");
        int r = sc.nextInt();
        System.out.print("Enter the number of terms in the series: ");
        int n = sc.nextInt();
        System.out.println("The sum of the series is: " + calcSum(a, r, n));
    }

    private static double calcSum(int a, int r, int n)
    {
        double sum = 0;
        if (n == 1) 
        {
            return a;
        } else if (r < 0) 
        {
            sum = (a * (Math.pow(r, n) - 1)) / (r - 1);
        } else if (r > 0)
        {
            sum = (a * (1 - Math.pow(r, n))) / (1 - r);
        }
        return sum;
    }
}

Output:

Enter the first term of the series: 1
Enter the common ratio: 2
Enter the number of terms in the series: 5
The sum of the series is: 31.0

Method-2: Java Program to Find Sum of Geometric Progression By Using User Input Value Without Using the Sum Formula

Approach:

  • Take the user input for the first term, common difference, and the number of terms.
  • Initialize sum variable as 0.
  • Use a for loop for i = 0 -> n.
  • Inside the for loop update the sum variable as sum += a * Math.pow(r, i). Math.pow() method is used find the power of a number.
  • Return sum.
  • Print the result.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first term of the series: ");
        int a = sc.nextInt();
        System.out.print("Enter the common ratio: ");
        int r = sc.nextInt();
        System.out.print("Enter the number of terms in the series: ");
        int n = sc.nextInt();
        System.out.println("The sum of the series is: " + calcSum(a, r, n));
    }

    private static double calcSum(int a, int r, int n)
    {
        double sum = 0;
        for (int i = 0; i < n; i++) 
        {
            sum += a * Math.pow(r, i);
        }
        return sum;
    }
}

Output:

Enter the first term of the series: 1
Enter the common ratio: 2
Enter the number of terms in the series: 5
The sum of the series is: 31.0

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Recommended Reading On: Python Program for Arithmetic Progression: Python Program for Arithmetic Progression

Answer these:

  1. Geometric Sequence Program In Java 1 2, 4, 8, 16.
  2. Java Program For Geometric Sequence.
  3. Gp Program In Java.
  4. 1 2 4 8 16 Series In Java.
  5. Sum Of Gp Formula.
  6. Gp In Java
  7. Gp Sum Formula
  8. Sum Of Gp

Related Java Programs: