Printing The Four Basic Shapes in Java

Problem:

Print the shapes seen below.


Pattern I:


    int MAX = 5;
    for (int i =0;i<=MAX;i++)
    {
      for(int j =0;j<=i;j++)
      {
        System.out.print((j+1)+" ");
      }
      System.out.println();
    }


Pattern II:


    int MAX =5;
    for (int i =MAX;i>=0;i--)
    {
      for(int j =0;j<=i;j++)
      {
        System.out.print((j+1)+" ");
      }
      System.out.println();
    }


Pattern III:


    int MAX =5;    
    for (int i =MAX;i>=0;i--)
    {
      for(int j =0;j<i;j++)
      {
        System.out.print("  ");
      }
      
      for(int k =MAX;k>=i;k--)
      {
        System.out.print(k-i+1" ");
      }
      System.out.println();
    }


Pattern IV:

    int MAX = 6;
    for (int i =0;i<MAX;i++)
    {
      for (int j =0;j<i;j++)
      {
        System.out.print("  ");
      }
      
      for (int k =MAX;k>i;k--)
      {
        System.out.print(MAX-k+1+" ");
      }
      

      System.out.println();
    }





No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact