Wednesday, April 2, 2014

Java program for pyramid pattern in java

import java.*;
class ZeroOneDiamond
{
public static void main(String[] args)
{
int n,i,j,k;
if(args.length>0)
{
for(i=0;i<args.length;i++)
{

try
{
n=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
n=0;
}
if(n<0) n=-n;
for(i=0;i<n;i++)
{
for(j=0;j<40-i;j++)
{
System.out.print(" ");
}
for(k=0;k<=i;k++)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}


for(i=n-1;i>0;i--)
{
for(j=40-i;j>=0;j--)
{
System.out.print(" ");
}
for(k=i;k>0;k--)
{
if(k%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
System.out.print("\n");
}

}
}
}
}

No comments:

Post a Comment