Sum of Arithmetic Progression
This program calculates sum of arithmetic progression series.
Problem Statement
Write a Java program that calculates sum of arithmetic progression with first term 2, difference 3, and 5 terms.
Source Code
| 1 | public class SumAP { |
| 2 | public static void main(String[] args) { |
| 3 | int a = 2, d = 3, n = 5, sum = 0; |
| 4 | for(int i=0; i<n; i++) { |
| 5 | sum += (a + i*d); |
| 6 | } |
| 7 | System.out.println("Sum of AP = " + sum + " - Eduinq"); |
| 8 | } |
| 9 | } |
Program Output
Sum of AP = 40 - Eduinq
Explanation
The program calculates each term using formula a + i*d and adds to sum. The loop runs for n terms. This demonstrates how for loops can calculate sum of arithmetic progression. Shows how for loop can calculate sum of AP series.