Sequential Number Diamond

This program prints a diamond of sequential numbers using nested for loop.

Problem Statement

Write a Java program that prints a diamond of sequential numbers up to 5 rows.

Source Code

1 public class NumberDiamond {
2 public static void main(String[] args) {
3 int num = 1;
4 for(int i=1; i<=5; i++) {
5 for(int j=1; j<=i; j++) {
6 System.out.print(num + " ");
7 num++;
8 }
9 System.out.println();
10 }
11 for(int i=4; i>=1; i--) {
12 for(int j=1; j<=i; j++) {
13 System.out.print(num + " ");
14 num++;
15 }
16 System.out.println();
17 }
18 }
19 }

Program Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 
20 21 22 
23 24 
25 

Explanation

The first half builds increasing rows, second half decreases rows, forming a diamond. Numbers increment sequentially. Shows how nested loops can generate numeric diamonds.

Quick Links to Explore