C program to print reversed mirrored right triangle star pattern

Write a C program to print reversed mirrored right triangle star(*) pattern series of n rows using for loop. How to print mirrored right triangle star pattern using for loop in C programming. The pattern with 5 rows should look like:
*****
 ****
  ***
   **
    *

Required knowledge:

Basic C programming, For loop
Before going through this pattern you must go through my previous post how to print inverted right triangle star pattern as both are similar in nature.

Logic:

If you look to above pattern and inverted right triangle star pattern you will find that both are similar with one exception. This pattern contains extra trailing spaces. Spaces are also arranged in a specific pattern i.e. each row contains total row_number - 1 spaces.

Program:

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

#include <stdio.h>

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

    //Reads number of rows to be printed from user
    printf("Enter value of n: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        //Prints trailing spaces
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

        //Prints right triangle
        for(j=i; j<=n; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
} 


Output
X
_
Enter value of n: 5
*****
 ****
  ***
   **
    *


Screenshot:

Reversed mirrored right triangle star pattern program in C


Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment