ICSE Class 10 Computer Applications Question 5 of 22

Nested for loops — Question 5

Back to all questions
5
Question

Question 2(iv)

Write a program in Java to display the following patterns.

1 * * * *
2 2 * * *
3 3 3 * *
4 4 4 4 *
5 5 5 5 5
public class KboatPattern
{
    public static void main(String args[]) {

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i);
            }

            for (int k = 1; k <= 5 - i; k++) {
                System.out.print('*');
            }
            System.out.println();
        }
    }
}
Output
BlueJ output of KboatPattern.java
Answer