KnowledgeBoat Logo

Computer Applications

Write a program in Java to display the following pattern:

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

Java

Java Nested for Loops

8 Likes

Answer

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

Output

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

Answered By

3 Likes