Java Pattern Programs

Write a program in Java to display the following pattern:

1 
9 25 49 
81 121 169 225 289

Java

Java Nested for Loops

5 Likes

Answer

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

Output

Answered By

2 Likes