mathajax

Sum of Arithmetic and Geometric Sequence

Sum of Arithmetic Sequence

The java program generates arithmetic sequence of  K numbers with common difference d.
The user has to give some input integer value to generate the sequence of numbers while running the program. These inputs are stored into variables a ,d and K.

a  - represents sequence first item
d  - sequence items common difference and
K - number of items in the sequence.
         

Arithmetic sequence =

sum of sequence 






import java.util.Scanner;

public class Sumarsequence {

 public static void main(String[] args) 
    {
    
 System.out.println("Sum of Arithmetic Sequence \n");  
 Scanner sc = new Scanner(System.in);
  
 System.out.println( "first value in sequence");
 int a = sc.nextInt();
  
 System.out.println( "common deference of sequence");
 int d = sc.nextInt();
  
 System.out.println( "K number of items in the sequence");
 int K = sc.nextInt();
 sc.close();  
  
 System.out.println(" Sequence ");  
  int sm=0; int n=0; 
  while ( n < K )   
  {      
   int s =  n*d + a;
   sm = sm+ s;
   System.out.print( s + ", ");
   n++;
  }

  System.out.println("\nSum of Sequence :" + sm);

 }

}


Output
Sum of Arithmetic Sequence 

first value in sequence
2
common deference
2
K number of items in the sequence
10

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 

Sum of Sequence :110

Sum of Geometric Sequence

The java program generates geometric sequence of  K numbers having common ratio r.

The user has to give some input integer value to generate the sequence of numbers while running the program. These inputs are stored into variables a ,cr and K.

a  - represents sequence first item
cr  -  item's common ratio and
K - number of items in the sequence.
Geometric sequence =


sum of sequence 






import java.util.Scanner;
public class SumgeoSequence {

 public static void main(String[] args) 
 {
      
   System.out.println("Sum of Geometric Sequence n");  
   Scanner sc = new Scanner(System.in);
  
   System.out.println("Enter first item(integer) in sequence");
   int a = sc.nextInt();
  
   System.out.println( "Enter common ratio of the sequence");
   int cr = sc.nextInt();
  
   System.out.println("K items in the sequence");
   int K = sc.nextInt();
   sc.close();
     
   System.out.println("\n Sequence ");
   int n=0; int sm=0;
     
  while( n<K ) 
  {
   int s = a* (int)Math.pow(cr, n) ;
   sm =sm + s;
   System.out.print(s+", ");  
   n++;
  }
  
 System.out.println("\n Sum of Geometric Sequence :" + sm);

 }

}


Output
Sum of Geometric Sequence 

Enter first item(integer) in sequence
2
Enter common ratio 
2
K items in the sequence
10

Sequence 
2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 
 Sum of Geometric Sequence :2046

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Matrix Determinant by Upper Triangular Matrix

Chebyshev distance between two points