Pin It

How to calculate Nth term and Sum of Arithmetic Progression in Java ?.

Program to calculate Nth term and Sum of Arithmetic Progression in Java

import java.util.Scanner;

public class ArithmeticProgression {
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);

    double firstTerm = 0;
    double numberOfTerms = 0;
    double nthTerm = 0;
    double commonDifference = 0;
    double  sum= 0;
    double term = 0;

    System.out.print("Enter the value of a (First Term) : "); 
    firstTerm = input.nextDouble();

    System.out.print("Enter the value of d (Common Difference) : "); 
    commonDifference = input.nextDouble();

    System.out.print("Enter the value of n (Number of terms) : "); 
    numberOfTerms = input.nextDouble();

    nthTerm = firstTerm + (numberOfTerms - 1) * commonDifference;

    sum = numberOfTerms * (2 * firstTerm + (numberOfTerms - 1) * commonDifference)/2;

    System.out.println("");
    System.out.println("The Arithmetic Progression is as follows :");

    for(int i = 0; i < numberOfTerms; i++){
      term = firstTerm + i * commonDifference;
      System.out.print(term+" + ");
    }

    System.out.println("...");
    System.out.println("The nthTerm of the series : " + nthTerm);
    System.out.println("The Sum of n terms of series : " + sum);
  }
}
Output of the program : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot