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