Thursday, February 13, 2014

Nested Loop - II [ Patterns ]




import java.io.*;
    public class Pattern_1
        {
            public static void main(String args[])throws IOException
                {
                    int n;
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("\n\tEnter the Size. : ");
                    n=Integer.parseInt(Br.readLine());
                    Pattern_1 NP=new Pattern_1();
                    System.out.println("\n\tNumber Pattern-1 ");
                    NP.Pat_1(n);
                    System.out.println("\n\tNumber Pattern-2 ");                   
                    NP.Pat_2(n);
                    System.out.println("\n\tNumber Pattern-3 ");                      
                    NP.Pat_3(n);
                    System.out.println("\n\tNumber Pattern-4 ");                      
                    NP.Pat_4(n);   
                    System.out.println("\n\tCharacter Pattern ");                      
                    NP.Pat_5(n);                       
               }
            void Pat_1(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int j=1;j<=n;j++)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                }
            void Pat_2(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int j=1;j<=i;j++)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                }    
            void Pat_3(int n)
                {
                    for(int i=n;i>=1;i--)
                        {
                            for(int j=n;j>=i;j--)
                                {
                                    System.out.print(i+" ");
                                }
                    System.out.println();           
                        }
                } 
            void Pat_4(int n)
                {
                    int space=-1,k;
                    for(int i=n;i>=1;i--)
                        {
                            for(int j=1;j<=i;j++)
                                {
                                    System.out.print(j);
                                }
                            for(int j=1;j<=space;j++)
                                {
                                    System.out.print(" ");
                                }
                            space+=2;
                            if(i==n)
                                {
                                    k=i-1;
                                }
                            else
                                {
                                    k=i;
                                }
                            for(int j=k;j>=1;j--)
                                {
                                    System.out.print(j);
                                }
                            System.out.println();           
                        }
                }                 
            void Pat_5(int n)
                {
                    for(int i=1;i<=n;i++)
                        {
                            for(int k=1;k<=n-i;k++)
                                {
                                    System.out.print(" ");
                                }
                            for(int j=1;j<i*2;j++)
                                {
                                    System.out.print("*");
                                }
                    System.out.println();           
                        }
                }
            }           

Output

          Enter the Size. : 5

          Number Pattern-1
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

          Number Pattern-2
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

          Number Pattern-3
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1

          Number Pattern-4
123454321
1234 4321
123   321
12     21
1       1

          Character Pattern
    *
   ***
  *****
 *******
*********

No comments: