C program to print mirrored right triangle star pattern

Write a C program to print mirrored right triangle star(*) pattern series using for loop. How to print right triangle star pattern series with trailing spaces of n rows using for loop in C programming. Logic to print mirrored right triangle star pattern series in C program.

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, For loop

Must know - Program to print simple right triangle star pattern

Logic to print mirrored right triangle star pattern

If you have gone though the previous post to print the right triangle star pattern then printing this wouldn't be difficult. For printing this, you need to add an extra inner loop that will print spaces before * gets printed. If you carefully look to the spaces you will find a special pattern of spaces that are eventually in decreasing order of row (i.e. first row contains n-1=4 spaces, followed by 3 and so on... where n is the total number of rows).

Program to print mirrored right triangle star pattern

/**
 * C program to print mirrored right triangle star pattern series
 */

#include <stdio.h>

int main()
{
    int i, j, n;

    // Input rows from user
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        // Print spaces in decreasing order of row
        for(j=i; j<n; j++)
        {
            printf(" ");
        }

        // Print star in increasing order or row
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        printf("\n");
    }
    
    return 0;
}

Output

Enter number of rows: 5
*
**
***
****
*****

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>