Number Grid

This program prints a 3x3 number grid using nested for loop.

Problem Statement

Write a Java program that prints a 3x3 grid of numbers using nested for loop.

Source Code

1 public class NumberGrid {
2 public static void main(String[] args) {
3 for(int i=1; i<=3; i++) {
4 for(int j=1; j<=3; j++) {
5 System.out.print(j + " ");
6 }
7 System.out.println();
8 }
9 }
10 }

Program Output

1 2 3 
1 2 3 
1 2 3 

Explanation

The outer loop controls rows, and the inner loop prints numbers 1 to 3 in each row. This demonstrates how nested loops can generate grids. Shows how nested for loops can print number grids.

Quick Links to Explore