Java program to display diamond shape using 0s and 1s using given limit

Java program to display diamond shape using 0s and 1s using given limit




import java.io.*;
import java.util.*;
public class sanszeroone
{
public static void main(String args[])
{
int limit,i,j,space,k;
Scanner s=new Scanner(System.in);
System.out.print("Enter the limit as odd number\t:");
limit=s.nextInt();
System.out.println();

for(i=1,k=1;i<=limit;i+=2,k++)
{

for(space=k;space<=(limit/2);space++)
{
System.out.print("  ");

}

for(j=1;j<=i;j++)
{
if(j==1||j==i)
{
System.out.print("1 ");
}
else
{
System.out.print("0 ");
}
}
System.out.println("\n");
}
for(i=limit-2,k=k-2;i>=1;i-=2,k--)
{
for(space=k;space<=(limit/2);space++)
{
System.out.print("  ");

}

for(j=1;j<=i;j++)
{
if(j==1||j==i)
{
System.out.print("1 ");
}
else
{
System.out.print("0 ");
}
}
System.out.println("\n");
}
}

}

OUTPUT


ask your comments here.....
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment