Java program to print diamond pattern of numbers

Java program to print diamond pattern of numbers. 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.

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 non negative 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 Number5                                                                                                               

     1                                                                                                                          

    123                                                                                                                         

   12345                                                                                                                        

  1234567                                                                                                                       

 123456789                                                                                                                      

  1234567                                                                                                                       

   12345                                                                                                                        

    123                                                                                                                         

     1