inverted pyramid pattern in c

Inverted full pyramid pattern in C

Inverted full pyramid pattern is a popular programming exercise that involves printing a pyramid shape in reverse. In this pattern, the number of asterisks (*) in the first row is maximum and gradually decreases as we move towards the last row.

In this article, we will discuss how to print the inverted full pyramid pattern in C programming language. We will also provide the code and output for the same.

Let’s get started!

Code to Print Inverted Full Pyramid Pattern in C

#include <stdio.h>

int main() {
   int rows, i, j, space;

   printf("Enter the number of rows: ");
   scanf("%d", &rows);

   for(i = rows; i >= 1; i--) {
      for(space = 0; space < rows - i; space++)
         printf(" ");
      for(j = 2 * i - 1; j > 0; j--)
         printf("*");
      printf("\n");
   }

   return 0;
}

Explanation of the Code

  • We start by including the header file stdio.h which is required for input/output operations in C.
  • We declare the main() function which is the entry point of our program.
  • We declare the variables rows, i, j, and space.
  • We ask the user to input the number of rows of the inverted pyramid pattern.
  • We start a for loop with i initialized to rows. This loop is responsible for the number of rows in the pyramid pattern.
  • We start another for loop inside the first loop with space initialized to 0. This loop is responsible for printing the spaces before the asterisks in each row.
  • We print the required number of spaces using the printf() function.
  • We start another for loop with j initialized to 2 * i – 1. This loop is responsible for printing the asterisks in each row.
  • We print the required number of asterisks using the printf() function.
  • We move to the next line using the printf() function.
  • We repeat the process until the inverted pyramid pattern is complete.
  • We return 0 to indicate that the program has executed successfully.

Output

When we run the above code and enter the number of rows as 5, the output will be as follows:

Enter the number of rows: 5
*********
 *******
  *****
   ***
    *

Conclusion

Inverted full pyramid pattern is a challenging programming exercise that can help improve your logical and coding skills. With the help of the above code, you can easily print this pattern in C programming language.