Java program to make an arithmetic progression.


 Arithmetic Progression (AP) is a sequence of numbers in order in which the difference of any two consecutive numbers is a constant value. For example, the series of natural numbers: 1, 2, 3, 4, 5, 6,… is an AP, which has a common difference between two successive terms (say 1 and 2) equal to 1 (2 -1). Even in the case of odd numbers and even numbers, we can see the common difference between two successive terms will be equal to 2.



Common Difference in Arithmetic Progression

In this progression, for a given series, the terms used are the first term, the common difference between the two terms, and nth term. Suppose, a1, a2, a3, ……………., an is an AP, then; the common difference “ d ” can be obtained as;

d = a2 – a1 = a3 – a2 = ……. = an – an – 1

Where “d” is a common difference. It can be positive, negative or zero.

The code for this program is as follows;

package com.arithmetic;
import java.util.Scanner;

public class ArithmeticProgression {
public static void main(String[] args) {
int i, result;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of terms of the arithmetic progression to be printed:");
int n = scan.nextInt();
Scanner in = new Scanner(System.in);
System.out.println("Enter the starting term of the required progression:");
int a = in.nextInt();
Scanner ib = new Scanner(System.in);
System.out.println("Enter the common difference:");
int cd = ib.nextInt();
System.out.println("the required arithmetic progression is:");
System.out.println(+a);
for (i=0;i<=n;i++){
result = a + cd;
a = result;
System.out.println(+result);
}

}
}








No comments:

Post a Comment