C program to print hollow mirrored right triangle star pattern

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

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Must know -

Logic to print mirrored right triangle star pattern

As when you look to the pattern carefully you will find that this pattern is similar to hollow right triangle if we add trailing spaces before printing *. If you look to the spaces you will find that spaces are arranged in a special pattern of decreasing order of row (i.e. first row contains n-1=4 spaces and second row contains 3 and so on... where n is the number of rows to be printed). So talking about the program if we add an extra inner loop (that print spaces) to the hollow right triangle program then this pattern can be simply made.

Program to print hollow mirrored right triangle star pattern

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

#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 trailing spaces
        for(j=i; j<n; j++)
        {
            printf(" ");
        }

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