Swastik Pattern
C
Hard
5 views
Problem Description
* * * * * *
* * * * * *
* * * * * * * *
* * *
* * *
* * * * * * * *
* * * * * *
* * * * * *
Complex pattern - middle row, middle column, and corners. By combining multiple conditions.
Official Solution
#include <stdio.h>
int main() {
int n = 9; // total rows/columns (make it odd for a clear middle)
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
// Conditions for stars
if (
// Top-left corner
(i < n/3 && j < n/3) ||
// Top-right corner
(i < n/3 && j >= 2*n/3) ||
// Bottom-left corner
(i >= 2*n/3 && j < n/3) ||
// Bottom-right corner
(i >= 2*n/3 && j >= 2*n/3) ||
// Middle row or middle column
(i == n/2) || (j == n/2)
) {
printf("*");
} else {
printf(" ");
}
// Add space for better formatting
printf(" ");
}
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!