Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it will tell the nth term of A.P and the sum of n terms of A.P #684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions algebra/arithmetic_progression/java/arithmetic_progression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// For an arithmetic progression a + (a + d) + (a + 2d) + (a + 3d) + ...
// with a being the initial term and d being the common difference
import java.util.*;
public class arithmetic_progression {

// This function calculates the nth term
Expand All @@ -11,5 +12,16 @@ public static int nth_term(int a, int d, int n) {
public static int sum_of_first_n(int a, int d, int n) {
return ((n) * ((2 * a) + (n - 1) * d)) / 2;
}
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
System.out.println("Enter number of terms in arithmentic progression:");
int n=scn.nextInt();
System.out.println("Enter first term of Arithmentic progression:");
int a=scn.nextInt();
System.out.println("Enter the mean difference between each term:");
int d=scn.nextInt();
System.out.println(n+ "-th " + "term of an arithmetic progression is: " + nth_term(a,d,n) );
System.out.println("Sum of first "+ n + " terms is: "+ sum_of_first_n(a,d,n));

}
}