Skip to content

Commit d6f147b

Browse files
committed
feat: Print AP series with formatted output and series summary
🧠 Logic: - Take input: first term `a`, common difference `d`, and number of terms `n`. - `printAPSeries()` prints the AP series with arrows (→) between terms. - `displaySeriesInfo()` computes: - Last term: `a + (n - 1) * d` - Sum of series: `n * (a + lastTerm) / 2` - Display detailed series info: first term, difference, number of terms, last term, and total sum. Signed-off-by: Somesh diwan <[email protected]>
1 parent 93cecb2 commit d6f147b

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Section8LoopAB/src/AS.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.Scanner;
2+
3+
public class AS {
4+
public static void main(String[] args) {
5+
Scanner scanner = new Scanner(System.in);
6+
7+
System.out.println("Arithmetic Progression (AP) Series Generator");
8+
System.out.println("------------------------------------------");
9+
10+
// Get input values
11+
System.out.print("Enter first term (a): ");
12+
int firstTerm = scanner.nextInt();
13+
14+
System.out.print("Enter common difference (d): ");
15+
int commonDiff = scanner.nextInt();
16+
17+
System.out.print("Enter number of terms (n): ");
18+
int numTerms = scanner.nextInt();
19+
20+
// Print the series
21+
System.out.println("\nGenerated AP Series:");
22+
printAPSeries(firstTerm, commonDiff, numTerms);
23+
24+
// Print additional information
25+
System.out.println("\n\nSeries Information:");
26+
displaySeriesInfo(firstTerm, commonDiff, numTerms);
27+
28+
scanner.close();
29+
}
30+
31+
private static void printAPSeries(int a, int d, int n) {
32+
int term = a;
33+
for (int i = 1; i <= n; i++) {
34+
System.out.print(term);
35+
if (i < n) {
36+
System.out.print(" → ");
37+
}
38+
term += d;
39+
}
40+
}
41+
42+
private static void displaySeriesInfo(int a, int d, int n) {
43+
// Calculate last term
44+
int lastTerm = a + (n - 1) * d;
45+
46+
// Calculate sum of series
47+
int sum = (n * (a + lastTerm)) / 2;
48+
49+
System.out.println("First Term (a): " + a);
50+
System.out.println("Common Difference (d): " + d);
51+
System.out.println("Number of Terms (n): " + n);
52+
System.out.println("Last Term: " + lastTerm);
53+
System.out.println("Sum of Series: " + sum);
54+
}
55+
}
56+
57+
/*
58+
Key Concepts of AP:
59+
1. First Term (a): The starting number of sequence
60+
2. Common Difference (d): The constant difference between consecutive terms
61+
3. Number of Terms (n): How many numbers in the sequence
62+
4. nth Term Formula: an = a + (n-1)d
63+
5. Sum Formula: Sum = (n/2)(first term and last term)
64+
65+
Real-world examples of AP:
66+
1. Natural numbers: 1, 2, 3, 4, 5... (a=1, d=1)
67+
2. Even numbers: 2, 4, 6, 8... (a=2, d=2)
68+
3. Odd numbers: 1, 3, 5, 7... (a=1, d=2)
69+
4. Counting backwards: 100, 95, 90, 85... (a=100, d=-5)
70+
71+
The program can be used to:
72+
- Generate a number of sequences
73+
- Calculate terms in a series
74+
- Find a sum of sequences
75+
- Model linear patterns
76+
- Solve problems involving constant change or growth
77+
78+
Common applications:
79+
- Financial calculations (fixed payments)
80+
- Distance calculations (constant speed)
81+
- Temperature changes (linear)
82+
- Grade progressions
83+
- Simple interest calculations
84+
*/

0 commit comments

Comments
 (0)