Print Right-angled Triangle Star Pattern in C and Java

Write a C and Java program to print Right-angled Triangle pattern formed by star(*) character.


 

This post covers below patterns formed by inverting Right-angled Triangle and its mirror –

 

 

Pattern 1: Inverted Right-angled Triangle


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

Here if we have n rows, first row will display n stars, 2nd row will display n-1 stars, 3rd row will display n-2 stars, and so on.. We can use two nested loops to print this pattern where outer loop represents row number (say i) and inner loop prints the star pattern (n – i + 1 times).

C

Download   Run Code

Java

Download   Run Code

 

Pattern 2: Mirror of Inverted Right-angled Triangle


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

Suppose we have n rows. First row will contain 0 space followed by n stars, 2nd row will contain 1 space followed by n-1 stars, 3rd row will contain 2 spaces followed by n-2 stars and so on.. We can use nested loops to print this pattern where outer loop represents row number (say i) and inner loop prints the space (i-1 times) followed by the star pattern (n-i+1 times).

C

Download   Run Code

Java

Download   Run Code

 

Pattern 3: Hollow and Inverted Right-angled Triangle


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

The idea remains same as Pattern 1 but here we print ‘*’ only for first row and first & last positions for remaining rows. We fill space for all other positions.

C

Download   Run Code

Java

Download   Run Code

 

Pattern 4: Mirror of Hollow and Inverted Right-angled Triangle


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

Here the idea remains the same as Pattern 2 but we print ‘*’ only for first row and first & last cell of remaining rows. All other positions will be filled by spaces.

C

Download   Run Code

Java

Download   Run Code

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Leave a Reply

avatar
  Subscribe  
Notify of