Heart Shape
C
Hard
6 views
Problem Description
*** ***
***** *****
***********
*********
*******
*****
***
*
Mathematical pattern - calculate specific coordinates where stars are printed.
Official Solution
#include <stdio.h>
int main() {
int i, j;
int rows = 8, cols = 11;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (
// Top two triangles
(i <= 2 && (j <= 2 + i || j >= 8 - i)) ||
// Middle full row
(i == 2) ||
// Bottom inverted triangle
(i > 2 && j >= i - 2 && j <= cols - (i - 1))
)
printf("*");
else
printf(" ");
}
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!