Advanced Numeric Sequence Grid

This program prints a 4x4 grid of alternating squares and cubes.

Problem Statement

Write a Java program that prints a 4x4 grid where even positions show squares and odd positions show cubes.

Source Code

1 public class AdvancedSequenceGrid {
2 public static void main(String[] args) {
3 int num = 1;
4 for(int i=1; i<=4; i++) {
5 for(int j=1; j<=4; j++) {
6 if(num % 2 == 0) {
7 System.out.print((num*num) + " ");
8 } else {
9 System.out.print((num*num*num) + " ");
10 }
11 num++;
12 }
13 System.out.println();
14 }
15 }
16 }

Program Output

1 4 27 16 
125 36 343 64 
729 100 1331 144 
2197 196 3375 256 

Explanation

The program increments num sequentially. If odd, cube is printed; if even, square is printed. Nested loops arrange results in grid form. Shows how nested loops can generate advanced numeric sequence grids.

Quick Links to Explore