Program to Create Geometric Sequence in java programming language by using user Input.

Geometric sequence program in java

GeometricSequence.java

import java.util.Scanner;
 class GeometricSequence
 {
     public static void main(String args[])
 {
 int n=5;   //limit

int r=2;  //common ratio

 int a=2;   //terms

 int sum=0;  //sum of the series

 Scanner sc=new Scanner(System.in);//take input from user

 System.out.print("Enter a first term:");

     a=sc.nextInt();

 System.out.print("Enter n limit:"); 

n=sc.nextInt(); 

System.out.print("Enter r common ratio : ");

 r=sc.nextInt(); for(int i=1;i<=n;i++)

{   
  System.out.print(""+a);

     a=a*r;     //multiply different with terms
      
         }    
     
      }
 }

/*OUTPUT
Enter a first term:8
 Enter n limit:6
 Enter r common ratio : 2
 8 16 32 64 128 256
 Process completed.   
*/
0Shares

Leave a Reply