Geometric Progression Series with While Loop
This program prints geometric progression series using while loop.
Problem Statement
Write a Java program that prints GP series with first term 3, ratio 2, and 6 terms using while loop.
Source Code
| 1 | public class GPWhile { |
| 2 | public static void main(String[] args) { |
| 3 | int a = 3, r = 2, n = 6, i = 0; |
| 4 | while(i < n) { |
| 5 | System.out.println((a * (int)Math.pow(r, i)) + " - Eduinq"); |
| 6 | i++; |
| 7 | } |
| 8 | } |
| 9 | } |
Program Output
3 - Eduinq 6 - Eduinq 12 - Eduinq 24 - Eduinq 48 - Eduinq 96 - Eduinq
Explanation
The program initializes first term a=3, ratio r=2, and prints 6 terms. The while loop increments i each time and calculates a * r^i. This demonstrates how while loops can generate geometric progression series. Shows how while loop can generate GP series.