Printing Asterisks Triangle Shapes in Java

Problem:

Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out.print( '*' ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to move to the next line. A statement of the form System.out.print( ' ' ); can be used to display a space for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.]

                     


Shape (a):

public class Exercice2a

{
 public static void main (String[] args)
 {
  for (int count =0; count < 10; count++)
  {
    for (int j=0; j < count+1; j++)
       System.out.print("*");
    System.out.println();
  }
 }
}

Shape (b):

public class Exercice2b

{
 public static void main (String[] args)
 {
  for (int count =11; count >= 0; count--)
  {
    for (int j=0; j < count-1; j++)
       System.out.print("*");
    System.out.println();
  }
 }
}

Shape (c):

public class Exercice2c

{
 public static void main (String[] args)
 {
        for(int count = 0; count < 10; count++)
        {
            for(int index=1; index < count+1; index++)
                System.out.print(" ");

            for(int star=10; star > count; star--)
                System.out.print("*");
            System.out.println();
        } 
    }
}

Shape (d):

public class Exercice2d

 {
 public static void main (String[] args)
  {
        for(int count = 10; count > 0; count--)
        {
            for(int index=0; index < count-1; index++)
                System.out.print(" ");
            
            for(int star=10; star > count-1; star--)
                System.out.print("*");
            
            System.out.println();
        }
 }


10 comments :

  1. how can i get this program, java asterisk forming a square
    * * * * *
    * *
    * *
    * *
    * *
    * * * * *

    ReplyDelete
  2. how to get this
    *** ***
    ** **
    * *

    ReplyDelete
  3. how to print this
    ********
    *** ***
    ** **
    * *
    * *
    ** **
    *** ***
    ********

    ReplyDelete
  4. The run of the mill or disconnected printing process. ThisVSL Print

    ReplyDelete
  5. Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. every door direct mail® postcard prints at 55printing

    ReplyDelete
  6. When compared to the typical/offline strategy this method provides a saving of 47%, however the printing quality is low and in the end, printing 250 sets at the office costs more than printing 1000 sets at an online printer.
    medijo

    ReplyDelete
  7. How can do this please help me(odd no. only)
    * * * * *
    * * *
    * * * * *
    * * *
    * * * * *

    ReplyDelete
  8. If you searching for the Avionics Displays , then they directly reach iDisplays. Here you people get up-to-date with the professional technical sales team. All of them explain everything about their displays deeply. Well, this display meets the precise technical and environmental specifications of the customer requirements.

    ReplyDelete

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