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 (x). Write a program in Java to display the following patterns.

1 2 3 4 5

2 3 4 5

3 4 5

4 5

5

4 5

3 4 5

2 3 4 5

1 2 3 4 5

Java Program: Java Program to Display the Pattern


Program Title: Mirror Number Pattern

public class MirrorNumberPattern {

    public static void main(String[] args) {

 

        // Upper half including middle

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

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

                System.out.print(j);

            }

            System.out.println();

        }

 

        // Lower half

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

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

                System.out.print(j);

            }

            System.out.println();

        }

    }

}

Output

12345
2345
345
45
5
45
345
2345
12345

Explanation

  • First loop prints decreasing numbers from 1 to 5
  • Second loop prints increasing numbers back
  • No spaces → output is exactly as required