Sitemap

Member-only story

Checkerboard Patterns in C

4 min readOct 27, 2024

1. Basic Checkerboard Pattern

This is a simple checkerboard pattern with alternating * and space characters.

c
Copy code
#include <stdio.h>
int main() {
int size = 8; // Size of the checkerboard (8x8)
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((i + j) % 2 == 0) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}

Expected Output:

markdown
Copy code
*   *   *   *   
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *

2. Checkerboard Pattern with Custom Size

This pattern allows the user to specify the size, creating a flexible checkerboard pattern.

c
Copy code
#include <stdio.h>
int main() {
int rows, cols;

printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if ((i + j) % 2 == 0) {…

--

--