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

This post covers the following patterns formed by inverting the Right-angled Triangle and its mirror:

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

Pattern 1: Inverted Right-angled Triangle

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

 
If we have n rows, the first row will display n stars; the 2nd row will display n-1 stars; the 3rd row will display n-2 stars, and so on. We can use two nested loops to print this pattern, where the outer loop represents row number (say i) and the 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. The first row will contain 0 spaces, followed by n stars, the 2nd row will contain 1 space, followed by n-1 stars, the 3rd row will contain 2 spaces, followed by n-2 stars, and so on. We can use nested loops to print this pattern, where the outer loop represents row number (say i) and the inner loop prints 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 the same as Pattern 1, but here we print ‘*’ only for the first row and first & last positions for the 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 the first row and the first & last cell of the remaining rows. Spaces will fill all other positions.

C


Download  Run Code

Java


Download  Run Code

That’s all about printing a right-angled triangle star pattern in C and Java.