C program to print reverse Pyramid star pattern

Write a C program to print reverse Pyramid or reverse equilateral triangle star(*) pattern series using for loop. How to print inverted pyramid or inverted equilateral triangle star pattern series using for loop in C programming. The pattern with 5 rows should look like:
*********
 *******
  *****
   ***
    *

Required knowledge:

Basic C programming, For loop

Logic:

The above pattern is similar to the simple pyramid star pattern in reversed order. To print this we need not to make much changes in simple pyramid star pattern we only need to reverse the loop conditions of the outer loop.

Program:

/**
 * C program to print reverse pyramid 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=n; i>=1; i--)
    {
        //Prints trailing spaces
        for(j=i; j<n; j++)
        {
            printf(" ");
        }

        //Prints reverse pyramid
        for(j=1; j<=(2*i-1); j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}


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


Screenshot:

Reverse Pyramid 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