- Write a C program to print a Hut star pattern.
Hut star pattern program’s output should be:
Required Knowledge
- For Loop
- printf and scanf function in C
Algorithm to print hut star pattern
Printing a hut star pattern is a two step process.
- Step 1: Print pyramid star pattern for top 5 rows. Check this c program to print pyramid star pattern.
- Step 2: Print the bottom half of the hut. Each row of the bottom half of hut contains 3 stars then three space characters followed by 3 stars.
Here is the matrix representation of the Hut star pattern. The row numbers are represented by i whereas column numbers are represented by j.
C program to print Hut star pattern on screen
#include<stdio.h> int main() { int i, j, space, rows = 8, star = 0; /* Printing upper triangle */ for (i = 0; i < rows; i++) { if (i < 5) { /* Printing upper triangle */ for (space = 1; space < 5 - i; space++) { printf(" "); } /* Printing stars */ while (star != (2 * i + 1)) { printf("*"); star++;; } star = 0; /* move to next row */ printf("\n"); } else { /* Printing bottom walls of huts */ for (j = 0; j < 9; j++) { if ((int) (j / 3) == 1) printf(" "); else printf("*"); } printf("\n"); } } return 0; }
Output
* *** ***** ******* ********* *** *** *** *** *** ***