KnowledgeBoat Logo

Computer Applications

Write a program in Java to display the following patterns.

#
* *
# # #
* * * * 
# # # # #

Java

Java Nested for Loops

3 Likes

Answer

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

Output

BlueJ output of Write a program in Java to display the following patterns. # * * # # # * * * * # # # # #

Answered By

1 Like


Related Questions