KnowledgeBoat Logo

Java Pattern Programs

Write a program in Java to display the following pattern:

1 3 5 7 9 
3 5 7 9 1 
5 7 9 1 3 
7 9 1 3 5 
9 1 3 5 7

Java

Java Nested for Loops

ICSE

1 Like

Answer

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

Output

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

Answered By

1 Like