Thursday, 1 October 2015

Star pattern in java programming #2

In the program the following star pattern will be printed.

 Click here for Star pattern in java programming #1

Output:

* * * * * * * * *
   * * * * * * * *
      * * * * * * *
         * * * * * *
            * * * * *
               * * * *
                  * * *
                     * *
                        *


/* Star pattern
 * 
 * */

package in.blogspot.java2bigdata;

import java.util.Scanner;  
    
public class StarPattern {  
       public static void main(String[] args)  
       {  
            int number; 
            System.out.println("Enter the number till you want a star pattern");  
            Scanner scan = new Scanner(System.in);  
            number = scan.nextInt();  
    
            label:     for(int i=0; i<number; i++)  
                 {  
                      for(int k=i; k>0; k--)  
                     {  
                           System.out.print(" ");  
                      }  
                      for(int j=0; j<number-i; j++)  
                      {  
                           System.out.print("*");  
                      }  
                     //for jump to next line  
                      System.out.println("");  
                 }  
       }  
  } 

At first, we get the number where we want to print the star pattern from the user with the use of Scanner class and store that number in variable number then we make a loop which starts from 0 till number-1.

Inside of that loop there is another loop which is used for giving spaces from left side equal to the line number.

There is one more loop inside the first loop which takes values from 0 to number-1 and used for printing stars and then there is a println statement which is used for going to the next line.



1 comment:

  1. It was clear to understand the concept of star pattern in Java programming.
    Regards,
    JAVA Training in Chennai

    ReplyDelete