Java program to calculate the sum of GP series.



Geometric Progression (G.P.):
A series of numbers in which ratio of any two consecutive numbers is always a same number that is constant. This constant is called as common ratio.

Example of G.P. series:
2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is 2.


package Demo;
import java.util.Scanner;
public class Seriese
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  float a, r, i, tn;
  int n;
  float sum = 0;

  System.out.print("Enter the first number of the G.P. series: ");
  a = sc.nextFloat();

  System.out.print("Enter the total numbers in the G.P. series: ");
  n = sc.nextInt();

  System.out.print("Enter the common ratio of G.P. series: ");
  r = sc.nextFloat();

  sum = (float)((a * (1 - Math.pow(r, n + 1))) / (1 - r));
  tn = (float)(a * (1 - Math.pow(r, n - 1)));

  System.out.printf("tn term of G.P.:", tn);
  System.out.printf("Sum of the G.P.:", sum);
 }
}

Output:
Enter the first number of the G.P. series: 4
Enter the total numbers in the G.P. series: 7
Enter the common ratio of G.P. series: 3
tn term of G.P.: -2912.000000
Sum of the G.P.: 13120.000000
BUILD SUCCESSFUL (total time: 14 seconds)


No comments:

Post a Comment