Geometric Progression Pyramid
This program prints a pyramid of geometric progression values using nested for loop.
Problem Statement
Write a Java program that prints a pyramid of GP values with first term 1, ratio 2, up to 4 rows.
Source Code
| 1 | public class GPPyramid { |
| 2 | public static void main(String[] args) { |
| 3 | int a = 1, r = 2, num = a; |
| 4 | for(int i=1; i<=4; i++) { |
| 5 | for(int j=1; j<=i; j++) { |
| 6 | System.out.print(num + " "); |
| 7 | num *= r; |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
1 2 4 8 16 32 64 128 256 512
Explanation
The outer loop controls rows, inner loop prints GP values sequentially. Each iteration multiplies num by ratio r. This demonstrates how nested loops can generate geometric progression in pyramid form. Shows how nested for loops can generate GP pyramids.