Computer Applications

Write a program in Java to display the following patterns.

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

Java

Java Nested for Loops

17 Likes

Answer

public class KboatPattern
{
    public static void main(String args[])  {
        char ch = '*';
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5 ; j++) {
                if(i == j)
                    System.out.print(i + " ");
                else
                    System.out.print(ch + " ");
            }
            System.out.println();
        }
    }
}

Output

Answered By

9 Likes


Related Questions