The nth term of an arithmetic sequence is given by. an = a + (n – 1)d. The number d is called the common difference because any two consecutive terms of an. the arithmetic sequence differs by d, and it is found by subtracting any pair of terms an and. an+1.
The code for this problem is:
import java.util.Scanner;
public class NthTermofAP {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first term of the arithmetic progression: ");
int a = scanner.nextInt();
System.out.println("Enter the common difference of the AP: ");
int cd = scanner.nextInt();
System.out.println("Enter the number of the term you want to find in the Ap the AP: ");
int n = scanner.nextInt();
int Tn = a + (n - 1 )*cd;
System.out.println("The "+n+"th term of the arithmetic progression is: "+Tn);
}
}
No comments:
Post a Comment