Hollow Diamond:
C
Easy
9 views
Problem Description
*
* *
* *
* *
* *
* *
*
Stars only on the boundaries, spaces in between. Combine the upper and lower triangles.
Official Solution
#include <stdio.h>
int main(void)
{
int n = 4; /* height of upper half */
/* Upper half */
for (int i = 1; i <= n; i++) {
/* Leading spaces */
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
/* Stars and inner spaces */
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1)
printf("*");
else
printf(" ");
}
printf("n");
}
/* Lower half */
for (int i = n - 1; i >= 1; i--) {
/* Leading spaces */
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
/* Stars and inner spaces */
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1)
printf("*");
else
printf(" ");
}
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!