KnowledgeBoat Logo

Computer Applications

Write a program in Java to display the following pattern:

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

Java

Java Nested for Loops

73 Likes

Answer

public class KboatPattern
{
    public static void main(String args[]) {
        for (int i = 5, j = 1; i >= 1; i--, j++) {
            for (int k = 1; k <= j; k++)
                System.out.print(i + " ");
            System.out.println();
        }
    }
}

Output

BlueJ output of Write a program in Java to display the following pattern: 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

Answered By

32 Likes