KnowledgeBoat Logo

Java Pattern Programs

Write a program in Java to display the following pattern:

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

Java

Java Nested for Loops

ICSE

24 Likes

Answer

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

Output

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

Answered By

10 Likes