Recent Tutorials and Articles
    Pattern printing in Java
    Published on: 2018-05-25 16:05:15

    A Java program to print the triangular shape pattern

    Pattern to print
    
              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
    
    Pattern1.java
    
    public class Pattern1 {
    
    	public static void main(String[] args) {
    		int noOfRows = 10;
    		char charcterToShow = '*';
    		char fillCharcter = '*';
    
    		for (int i = 0; i <= noOfRows; i++) {
    			for (int j = 0; j <= noOfRows + i; j++) {
    				if (j == (noOfRows - i) || j == (noOfRows + i)) {
    					System.out.print(charcterToShow);
    					continue;
    				}
    
    				if (j > (noOfRows - i)) {
    					System.out.print(fillCharcter);
    					continue;
    				}
    
    				System.out.print(" ");
    			}
    			System.out.println();
    		}
    
    	}
    
    }
    
    
    Output
    
    
              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
    
    

    You can easily change the size of pattern by increasing/decreasing the value of noOfRows variable. Additionally you can change the shape by changing the value of fillCharacter variable. E.g. updating the value of fillCharacter varibable to ^ will result into following output -

    Output
    
    
              *
             *^*
            *^^^*
           *^^^^^*
          *^^^^^^^*
         *^^^^^^^^^*
        *^^^^^^^^^^^*
       *^^^^^^^^^^^^^*
      *^^^^^^^^^^^^^^^*
     *^^^^^^^^^^^^^^^^^*
    *^^^^^^^^^^^^^^^^^^^*
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 2018-05-25 16:05:15

    Comment Form is loading comments...