-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathArithmetic_Progression.java
More file actions
47 lines (42 loc) · 1.29 KB
/
Arithmetic_Progression.java
File metadata and controls
47 lines (42 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
ARITHMETIC PROGRESSION
Any sequence of elements where the difference between any two
consecutive elements is equal is termed to be in A.P.
The nth term of an A.P. is defined as (a + (n - 1)*d)
where a is the first element of the A.P.
d is the common difference of the A.P.
*/
import java.util.Scanner;
class Arithmetic_Progression {
static int SumOfAP(int a, int d, int n) {
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
public static void main(String args[]) {
System.out.print("Enter the First Term of A.P.");
int a;
Scanner s = new Scanner(System.in);
a = s.nextInt();
System.out.print("Enter the common difference");
int d;
d = s.nextInt();
System.out.print("Enter N (The index of term to find)");
int n;
n = s.nextInt();
System.out.print("The term at index " + n + " is " + (a + (n - 1) * d));
System.out.print("Sum is: " + SumOfAP(a, d, n));
}
}
/*
INPUT : a = 5
d = 2
n = 7
OUTPUT : The term at index 7 is 17
Sum is: 77
VERIFICATION : The A.P. would be 5,7,9,11,13,15,17...
*/