Java program to display diamond pattern
Diamond pattern:
Program Example:
import java.util.Scanner;
public class Diamond
{
public static void main(String[] args)
{
int i,j,k,p,q,r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the values: ");
int n = sc.nextInt();
int spaces = n-1;
for(i=0;i<n;i++)
{
for(j=0;j<spaces;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
System.out.print("* ");
}
spaces--;
System.out.println();
}
spaces = 1;
for(p=(n-1);p>0;p--)
{
for(q=0;q<spaces;q++)
{
System.out.print(" ");
}
for(r=0;r<p;r++)
{
System.out.print("* ");
}
System.out.println();
spaces++;
}
}
}
Output:
Enter the values: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
"If you have any other logic to attain the same result please email the code to sharkrish50@gmail.com"
Diamond pattern |
Program Example:
import java.util.Scanner;
public class Diamond
{
public static void main(String[] args)
{
int i,j,k,p,q,r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the values: ");
int n = sc.nextInt();
int spaces = n-1;
for(i=0;i<n;i++)
{
for(j=0;j<spaces;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
System.out.print("* ");
}
spaces--;
System.out.println();
}
spaces = 1;
for(p=(n-1);p>0;p--)
{
for(q=0;q<spaces;q++)
{
System.out.print(" ");
}
for(r=0;r<p;r++)
{
System.out.print("* ");
}
System.out.println();
spaces++;
}
}
}
Output:
Enter the values: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
"If you have any other logic to attain the same result please email the code to sharkrish50@gmail.com"
Comments
Post a Comment