+ 1

Doubt in C

#include <stdio.h> int main() { int a, b, c; for(c = 1; c < 9; c++){ printf("\n"); if (c % 2 == 0){ for(b = 0; b < 4; b++){ printf("⬜️⬛️"); } } else{ for(a = 0; a < 4; a++){ printf("⬛️⬜️"); } } } printf("\n Lets Play!"); return 0; } . . . Ques: if(c%2==0); What does this line mean? Why c% ?

28th Nov 2024, 3:50 AM
𝓐𝓭𝓲𝓽𝓲⚝
𝓐𝓭𝓲𝓽𝓲⚝ - avatar
4 Answers
+ 3
it can be simpler: #include <stdio.h> int main() { for(int i=1; i<9; i++){ if(i%2==0) puts("⬜️⬛️⬜⬛⬜⬛⬜⬛"); else puts("⬛️⬜️⬛⬜⬛⬜⬛⬜"); } puts(" Lets Play!"); }
28th Nov 2024, 4:47 AM
Bob_Li
Bob_Li - avatar
+ 2
c%2==0 is a way to check if c is even. c=0, 2,4,6,8 This is so that we can alternate between printing ⬜⬛⬜⬛⬜⬛⬜⬛ if(c%2==0) and ⬛⬜⬛⬜⬛⬜⬛⬜ (else) to print the checkerboard pattern
28th Nov 2024, 4:20 AM
Bob_Li
Bob_Li - avatar
+ 2
This line means if c divided by 2 and remainder is equals to zero then execute the code written inside if statement else skip the if statement if condition is false.
29th Nov 2024, 9:47 AM
𝒮𝑜𝒽𝒶𝒾𝒷
𝒮𝑜𝒽𝒶𝒾𝒷 - avatar
0
Hi
29th Nov 2024, 10:39 PM
Yusif Ibrahim Sillah