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: Program to Print the Pattern


Write a program to generate the following output.

@

@ #

@ # @

@ # @ #

@ # @ # @

public class StarHashPattern {

    public static void main(String[] args) {

 

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

 

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

 

                if (j % 2 == 1)

                    System.out.print("@ ");

                else

                    System.out.print("# ");

            }

 

            System.out.println();

        }

    }

}

Output

@

@ #

@ # @

@ # @ #

@ # @ # @

Explanation

🔹 Outer Loop (i)

  • Controls the number of rows (1 to 5).

🔹 Inner Loop (j)

  • Controls the number of elements in each row.
  • Runs from 1 to i.

🔹 Condition

if (j % 2 == 1)

  • Odd position → prints @
  • Even position → prints #

🔹 Line Break

System.out.println();

  • Moves output to the next line after each row.

Key Concepts Used

Nested for loops
Conditional operator (if-else)
Pattern programming