Diamond pattern in Java
Written by
Java program to print diamond pattern
We are going to create a pattern that looks like a diamond, somewhat resembling the diamonds we find in a deck of playing cards. The aforesaid has to be created out of any character given as input.
The basic idea is to first print a Pascal Triangle out of input character and then print an inverted Pascal Triangle as a continuation below it.
Source Code:
/* Program to print Diamond*/
import java.util.*;
class Diamond
{
public static void main()
{
Scanner inp=new Scanner(System.in);
System.out.print("\n Enter Any Character: ");
char c=((inp.nextLine()).charAt(0));
System.out.print("\n Enter Size Limit: ");
int n=inp.nextInt();
int i,j,x=(n-1);
System.out.println("Diamond: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=x;j++)
System.out.print(" ");
x--;
for(j=1;j<=(2*i)-1;j++)
{
System.out.print(c);
}
System.out.println();
}
x=1;
for(i=1;i<=(n-1);i++)
{
for(j=1;j<=x;j++)
System.out.print(" ");
x++;
for(j=1;j<=2*(n-i);j++)
System.out.print(c);
System.out.println();
}
}
}
Output:
Enter Any Character: #
Enter Size Limit: 5
Diamond:
#
#######
#########
#######
#
Enter Any Character: &
Enter Size Limit: 4
Diamond:
&
&&&
&&&&&
&&&&&&&
&&&&&
&&&
&