Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Nested for Loops in Java

Chapter 10

Nested for Loops in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Java Program to Display the Pattern


2 (ix). Write a program in Java to display the following patterns.

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Java Program: Java Program to Display the Pattern


public class MirrorNumberPattern {

    public static void main(String[] args) {

 

        // Upper decreasing part

        for (int i = 5; i >= 1; i--) {

            for (int j = 1; j <= i; j++) {

                System.out.print(j + " ");

            }

            System.out.println();

        }

 

        // Lower increasing part

        for (int i = 2; i <= 5; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print(j + " ");

            }

            System.out.println();

        }

    }

}

Output

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Explanation

🔹 First Loop (Decreasing Pattern)

for (int i = 5; i >= 1; i--)

  • Prints rows with numbers from 1 to i
  • Creates the top inverted triangle

🔹 Second Loop (Increasing Pattern)

for (int i = 2; i <= 5; i++)

  • Starts from 2 to avoid repeating the middle line
  • Creates the bottom triangle

🔹 Inner Loop

for (int j = 1; j <= i; j++)

  • Prints numbers starting from 1

Key Concepts Used

Nested for loops
Pattern symmetry
Loop control