Pages

Java Asterisk Pattern using Loops - Pattern One

        To understand better about loops, asterisk patterns will be a very big help for aspiring programmers. There are lots of patterns to form using loops in Java (while, do while, for loops). One can even draw shapes, text, figures and others. Today, I will show you some of the asterisk patterns in Java.

       
public class PatterOne
{

 public static void main(String[]args){  
   for (int j = 7; j > 0; j--){
                System.out.println("");
   for (int i = 5; i >= j; i--){
                System.out.print("*");
            }
            }
  System.out.println("");

 }

}

Here is the sample output of the code above:



4 comments:

  1. how about this
    *
    ***
    ******
    ***
    *
    ?

    ReplyDelete
    Replies
    1. for(int i=0;i<5;i=i+2)
      {
      for(int j=0;j<=i;j++)
      {
      System.out.print("*");
      }
      System.out.println();
      }
      for(int i=3;i>0;i=i-2)
      {
      for(int j=i;j>0;i--)
      {
      System.out.print("*");
      }
      System.out.println();
      }

      Delete
    2. in 6th last statement, it will be 'j--', not 'i--' !!!

      Delete