Factorial Summation Grid
This program prints a 3x3 grid of factorial values using nested for loop.
Problem Statement
Write a Java program that prints factorials of numbers 1-9 in a 3x3 grid using nested for loop.
Source Code
| 1 | public class FactorialGrid { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 1; |
| 4 | for(int i=1; i<=3; i++) { |
| 5 | for(int j=1; j<=3; j++) { |
| 6 | int fact = 1; |
| 7 | for(int k=1; k<=num; k++) { |
| 8 | fact *= k; |
| 9 | } |
| 10 | System.out.print(fact + "\t"); |
| 11 | num++; |
| 12 | } |
| 13 | System.out.println(); |
| 14 | } |
| 15 | } |
| 16 | } |
Program Output
1 2 6 24 120 720 5040 40320 362880
Explanation
The outer loops generate a grid, while the inner loop calculates factorial for each number. This demonstrates factorial summation grids using nested loops. Shows how nested loops can generate factorial grids.