C program to print hollow mirrored inverted right triangle star pattern

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

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Must know - Program to print hollow inverted right triangle star pattern

Logic to print hollow mirrored inverted right triangle star pattern

If you look to the pattern carefully you will find that it is similar to hollow inverted right triangle if we add trailing spaces. So what we need is we need to make a little change in the hollow inverted right triangle star pattern program, we need to add an extra inner loop that will print spaces before the star gets printed. If you look to the spaces you will notice they are in a special pattern (i.e. spaces per row is spaces = row_number - 1).

Program to print hollow mirrored inverted right triangle star pattern

/**
 * C program to print hollow mirrored inverted right triangle star pattern
 */

#include <stdio.h>

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

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

    for(i=1; i<=n; i++)
    {
        // Print trailing spaces before star gets printed
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

        // Print hollow inverted right triangle
        for(j=i; j<=n; j++)
        {
            if(j==i || j==n || i==1)
            {
                printf("*");
            }
            else
            {
                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>