Sandglass Pattern
C
Hard
5 views
Problem Description
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Inverted triangle above, normal below. Mirror image vertically.
Official Solution
#include <stdio.h>
int main() {
int n = 5; // height of triangle
int i, j;
for (i = 0; i < 2*n - 1; i++) {
int spaces = (i < n) ? i : (2*n - 2 - i);
int stars = n - spaces;
// leading spaces
for (j = 0; j < spaces; j++)
printf(" ");
// stars
for (j = 0; j < stars; j++)
printf("* ");
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!