KnowledgeBoat Logo

Computer Applications

Write a program in Java to display the following patterns.

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

Java

Java Nested for Loops

3 Likes

Answer

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("  ");
            }
            for (int k = i; k <= 5; k++) {
                System.out.print(k + " ");
            }
            System.out.println();                  
        }
        for(int i = 4; i >= 1 ; i--) {
            for (int j = 1; j < i; j++) {
                System.out.print("  ");
            }
            for (int k = i; k <= 5; k++) {
                System.out.print(k + " ");
            }
            System.out.println();                  
        }
    }
}

Output

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

Answered By

2 Likes


Related Questions