Geometric Progression Pyramid with Powers of 3
This program prints a pyramid of powers of 3 using nested for loop.
Problem Statement
Write a Java program that prints a pyramid of powers of 3 up to 4 rows using nested for loop.
Source Code
| 1 | public class GPPyramidThree { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 1; |
| 4 | for(int i=1; i<=4; i++) { |
| 5 | for(int j=1; j<=i; j++) { |
| 6 | System.out.print(num + " "); |
| 7 | num *= 3; |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
1 3 9 27 81 243 729 2187 6561 19683
Explanation
The outer loop controls rows, inner loop prints GP values sequentially. - Each iteration multiplies num by 3. - This demonstrates how nested loops can generate geometric progression pyramids with powers of 3. Conclusion: Shows how nested for loops can generate GP pyramids with powers of 3.