In this article, I will show you, How to write a C program to print hollow inverted pyramid star pattern series of n rows using for loop. How to print hollow inverted pyramid star pattern in C programming. Here, one thing is important to know that the rows of the pyramid.
Logic to C program to print hollow inverted pyramid star pattern:
- Enter the row value for the equilateral triangle.
- Here I have used three loops one is the outer loop to change the line and two inner loops to print star and space.
- The outer loop iterate N times and print a newline after completing the inner loop.
- First inner loop print the space x to (N-1)(where N is the row number and x is the current row number).
- The second inner loop print the star at xth or last column or for last row. It iterates from 1 to ( ( N* 2) – ( (2*x) -1) ) times.
See the, C program to print hollow reverse pyramid star pattern:
#include <stdio.h> int main() { int x = 0,y = 0; unsigned int rows = 0; printf("Enter the number of rows = "); scanf("%u",&rows); for(x=1; x<=rows; ++x) { // Print spaces for(y=1; y<=x; ++y) { printf(" "); } // Print star/ for(y =1; y <=((rows*2)-((2*x)-1)); ++y) { if(x==1 || y==1 || y==((rows*2)-((2*x)-1))) { printf("*"); } else { printf(" "); } } // Print new line printf("\n"); } return 0; }
Output:
Code Analysis:
It asks the user to enter the row for the hollow reverse pyramid (equilateral triangle).
printf("Enter the number of rows = "); scanf("%u",&rows);
First, inner loop print space x to (N-1) times.
// Print spaces for(y=1; y<=x; ++y) { printf(" "); }
The second inner loop print the star at x-th position or last column or for the first row otherwise it prints space.
// Print star for(y =1; y <= ((rows*2)-((2*x)-1)); ++y) { if(x==1 || y==1 || y==((rows*2)-((2*x)-1))) { printf("*"); } else { printf(" "); } }
The outer loop print the newline after each iteration of the inner loops.
Recommended Post:
- C program to print pyramid star pattern.
- C program to Print Square Star Pattern.
- C program to print the mirrored right triangle star pattern.
- print hollow mirrored right triangle star pattern.
- How to use for loop in C.
- Use of if condition in C programs.
- File handling in C.
- C format specifiers.
- 100 C interview Questions.
- Pointer in C.
- Use of do-while in C.
- How to use fgetc in C?
- How to use fputc in C?