Zig-zag Pattern
C
Medium
5 views
Problem Description
* *
* *
* *
* *
*
* *
* *
* *
* *
Diagonal lines alternate directions. Row-column relationship formula.
Official Solution
#include <stdio.h>
int main() {
int i, j;
int n = 5; // middle row number
int totalCols = 2*n - 1;
// Top half
for (i = 1; i <= n; i++) {
for (j = 1; j <= totalCols; j++) {
if (j == i || j == totalCols - i + 1)
printf("*");
else
printf(" ");
}
printf("n");
}
// Bottom half
for (i = n-1; i >= 1; i--) {
for (j = 1; j <= totalCols; j++) {
if (j == i || j == totalCols - i + 1)
printf("*");
else
printf(" ");
}
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!