Wednesday 30 September 2015

Star pattern in java programming

In the program the following star pattern will be printed.

out put:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

/*     star pattern
 *  *
*  *
*  **
*  ***
*  ****
*  *****
*  ******
*  *******
*  ********
* */

package in.blogspot.java2bigdata;

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

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 n then we make a loop which starts from 0 till n-1.

Inside of that loop there is another loop from 0 to n-1 and we jump from the inner loop if we get a number greater than the outer loop value with the use of continue because we only want stars equal to outer loops in a single line.


Arrays's different outputs

In this post, you will get to know what are different outputs come while initializing an array.

Arrays in Java
Array creation