14 June 2018

print pattern

i/p:
6

o/p:
  1st quad   |    2nd quad -->for understating   
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0     
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 0 5 4 3 2 1 0
0 1 2 3 4 0 0 0 4 3 2 1 0
0 1 2 3 0 0 0 0 0 3 2 1 0
0 1 2 0 0 0 0 0 0 0 2 1 0
0 1 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0
3rd quad    |     4th quad  ->for understanding


public class NewClass19 {

    void pattern(int n)
    {
        int len=(2*n)+1;
        for(int i=0;i<len;i++)
        {     int c=0,k=i,l=len-i-1;
            for(int j=0;j<len;j++)
            {
                if(i<= n &&j<=i ) //1st quad
                {
                    System.out.print(j+" ");
                }
                else if( i<=n && j>=len-1-i) //2nd quad
                { 
                    if(i==n){ k--; System.out.print(k+" "); }
                    else
                    System.out.print((k--)+" ");
                }
                else if(i>n && j<(len-i)) //3rd quad
                {
                    System.out.print(j+" ");
                }
                else if(i>n && j>=i)  //4th quad
                {
                    System.out.print(l--+" ");
                }
                else
                    System.out.print(0+" ");
            }System.out.println();
        }
    }
   
public static void main(String args[])
    {
        int n=6;
        NewClass19 ob=new NewClass19();
        ob.pattern(n);
    }
}

No comments:

Post a Comment