Hourglass Pattern
C
Medium
5 views
Problem Description
*********
*******
*****
***
*
***
*****
*******
*********
Inverted triangle, then normal triangle. Coordination of spaces and stars
Official Solution
#include <stdio.h>
int main() {
int i, j;
int n = 5; // Height of the top inverted triangle
// Top inverted triangle
for (i = n; i >= 1; i--) {
// Print spaces
for (j = 1; j <= n - i; j++) {
printf(" ");
}
// Print stars
for (j = 1; j <= 2*i - 1; j++) {
printf("*");
}
printf("n");
}
// Bottom normal triangle
for (i = 2; i <= n; i++) {
// Print spaces
for (j = 1; j <= n - i; j++) {
printf(" ");
}
// Print stars
for (j = 1; j <= 2*i - 1; j++) {
printf("*");
}
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!