Search⌘ K
AI Features

Solution: FizzBuzz

Explore how to implement the classic FizzBuzz problem using control flow constructs in C. Learn to use for loops and conditional statements to print 'Fizz', 'Buzz', 'FizzBuzz', or numbers correctly, enhancing your understanding of loop and decision logic in C.

We'll cover the following...
C
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 100; i++) {
// If the number is divisible by 3, remainder will be 0
// We use the operator ! to change 0 to 1 so that the overall
// condition becomes true
if (!(i % 3) && !(i % 5)) // Check if it's divisible by 3 and 5 by taking % (modulo) 3 and 5
printf("FizzBuzz");
else if (!(i % 3)) // Check if it's divisible by just 3
printf("Fizz");
else if (!(i % 5)) // Check if it's divisible by just 5
printf("Buzz");
else
printf("%d", i);
printf("\n");
}
return 0;
}
...