C program to print hollow inverted right triangle star pattern

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

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Must know -

Logic to print hollow inverted right triangle star pattern

You will find this pattern easier if you are done with hollow right triangle and inverted right triangle star patterns. If you haven't here is a simple logic behind this pattern.

First is total number of characters(including stars and spaces) per row decreases as row number increases(i.e. first row contains 5 characters and afterwards second contains 4 and so on...) means each row contains n - row_number + 1 characters(where n is the total number of rows).

Second is stars(*) only gets printed for row=1 or column=1 or column=n(Where n is the total number of rows to be printed). And spaces gets printed when stars don't.

Program to print hollow inverted right triangle star pattern

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

#include <stdio.h>

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

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

    // Outer loop for iterating over rows
    for(i=1; i<=n; i++)
    {
        for(j=i; j<=n; j++)
        {
            /* Print stars only for row=1, column=1 or column=n */
            if(i==1 || j==i || j==n)
            {
                printf("*");
            }
            else
            {
                // Print spaces if stars don't get printed
                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>