Monday, 31 August 2015

Diamond pattern using JAVA

public class Diamond_pattern
{
/*
*     1
*   121   
* 12321
*   121      
*    1
*/

public static void main(String[] args)
{
int noofCols=1;
int noofspaces=2;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=noofspaces;j++)
{
System.out.print(" ");
}
if(i<3)
{
noofspaces=noofspaces-1;
}
else
{
noofspaces=noofspaces+1;
}
int k=0;
for(int j=1;j<=noofCols;j++)
{
if(j<=i)
{
k=k+1;
}else
{
k=k-1;
}
System.out.print(k);

}
System.out.println();
if(i<3)
{
noofCols=noofCols+2;
}
else
{
noofCols=noofCols-2;
}
}

}

}

1 comment:

  1. thank you sir for this pattern.
    but one problem is i am getting different last but one line (i.e 123 insted of 121)
    pls check it sir

    ReplyDelete