KnowledgeBoat Logo

Computer Applications

Write a program in Java to display the following pattern:

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Java

Java Nested for Loops

13 Likes

Answer

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

Output

BlueJ output of Write a program in Java to display the following pattern: 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15

Answered By

5 Likes