C program to print Right-angled Triangle Star Pattern

Write a C 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

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

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

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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz