********* * * * * * * *
Required knowledge:
Basic C programming, If else, For loopBefore moving on to this pattern you must be done with how to print inverted pyramid pattern and hollow pyramid patterns as both are recommended before this pattern.
Logic:
If you look to the pattern structure you will notice that you have to do two things with this pattern. First is you have to print a simple inverted pyramid and second is you have to make the inverted pyramid hollow. Printing inverted pyramid won't be difficult as I discussed in my previous post. And to make pyramid hollow you just have to apply a condition before printing stars i.e. print stars only when row=1 or column=1 or column=(2*rownumber - 1) otherwise print space.Program:
/** * C program to print hollow inverted 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 hollow pyramid for(j=1; j<=(2*i-1); j++) { if(i==n || j==1 || j==(2*i-1)) { printf("*"); } else { printf(" "); } } printf("\n"); } return 0; }
Output
Enter value of n: 5
*********
* *
* *
* *
*
*********
* *
* *
* *
*
Screenshot:
Happy coding ;)
You may also like
- All star patterns programs index.
- For loop programming exercises and solutions.
- If else programming exercises and solutions.
- C program to check whether a triangle is Equilateral, Scalene or Isosceles.
- C program to check whether a triangle is valid or not.
- C program to convert Binary to Decimal number system.
- C program to convert Octal to Decimal number system.
- C program to convert Decimal to Binary number system.
- C program to convert Hexadecimal to Octal number system.
- C program to print Pascal triangle of n rows.
- C program to print fibonacci series.
- C program to check whether a number is Prime number or not.