Friday, 29 August 2014

#1 Star Pattern Program in Java



In the program the following star pattern will be printed.

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.

The program is shown below :

1:  import java.util.Scanner;  
2:    
3:  class StarPattern1  
4:  {  
5:       public static void main(String[] args)  
6:       {  
7:            int n;  
8:            System.out.println("Enter the number till you want a star pattern");  
9:            Scanner scan = new Scanner(System.in);  
10:            n = scan.nextInt();  
11:    
12:            label:     for(int i=0; i<n; i++)  
13:                 {  
14:                      for(int j=0; j<n; j++)  
15:                      {  
16:                           if(j>i)  
17:                           {  
18:                                System.out.println();  
19:                                continue label;  
20:                           }  
21:                           else  
22:                           {  
23:                                System.out.print("*");  
24:                           }  
25:                      }  
26:                 }  
27:       }  
28:  }  

Output :





4 comments :


  1. Great Collection of Programs in Java


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


    How to Write this program to Print Star Pattern in Java

    ReplyDelete
    Replies
    1. Really Nice Post to Print Star Pattern in Java Yesterday I visit this blog and i was found lots of helpful information from your side thanks for sharing updated and New Technology related Post.

      Delete