Diamond Pattern in Java Using For Loop

Diamond pattern of numbers in java. Java Diamond pattern program for beginners to intermediate level java programmers, a diamond pattern program using a number in the java programming language. This program will print a pattern using numbers as the program’s outline.

For Loop in Java

In Java, a “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a specified number of times. The test condition supplied within the “for” loop determines the number of iterations. The Java “for” loop is one of the most basic Java loops to understand. 

In programming, a for loop is a conditional iterative statement that is used to check for specific criteria and then continually execute a block of code as long as those circumstances are met.

If Else Statement in Java

In Java, the if statement is used to execute a block of code if a given condition is true. If a condition evaluates to false, the if…else statement is used alongside an if statement to run code. Furthermore, the if…else…if statement is used to test numerous conditions.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter the Number");
n = scan.nextInt();
diamond(n);
}
private static void diamond(int n)
{
if(n <= 0)
System.out.println("Enter the numbers");
int a = 0;
for(int i=1; i <= n; i++) 
{
a = 1;
for(int j = i; j <= n; j++) 
{
System.out.print(" ");
}
for(int k = 1; k <= 2*i-1; k++) 
{
System.out.print(a++);
}
System.out.println();
}
for(int i=n-1; i >= 1; i--) 
{
a = 1;
for(int j=n; j >= i; j--) 
{
System.out.print(" ");
}
for(int k=1; k <= 2*i-1; k++) 
{
System.out.print(a++);
}
System.out.println();
}
}
}

Output:

Enter the Number 5

    1

  123

12345

  123

    1