FizzBuzz Program in C
This program prints numbers from 1 to 100, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and multiples of both with “FizzBuzz”.
Program Structure
The structure of the program is as follows:
- Include necessary libraries: The program includes the standard input-output library using
#include <stdio.h>
. - Main function: The program contains the main function where the logic is implemented.
- Loop through numbers 1 to 100: A
for
loop is used to iterate through numbers from 1 to 100. - Condition checks: Within the loop,
if
statements are used to check if a number is a multiple of 3, 5, or both, and print the corresponding output.
FizzBuzz Program
#include <stdio.h>
/**
* main - Entry point of the program
*
* Description:
* This function iterates through numbers 1 to 100 and prints "Fizz" for multiples
* of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.
* For other numbers, it simply prints the number itself.
*
* Return: Always 0 (Success)
*/
int main() {
for (int i = 1; i <= 100; i++) {
// Check if the number is a multiple of both 3 and 5
if (i % 3 == 0 && i % 5 == 0) {
printf("FizzBuzz\n");
}
// Check if the number is a multiple of 3
else if (i % 3 == 0) {
printf("Fizz\n");
}
// Check if the number is a multiple of 5
else if (i % 5 == 0) {
printf("Buzz\n");
}
// If the number is not a multiple of 3 or 5, print the number
else {
printf("%d\n", i);
}
}
return 0;
}
Explanation of the Program
The program starts by including the standard input-output library <stdio.h>
which is necessary for using the printf
function.
In the main
function, a for
loop is used to iterate through numbers from 1 to 100. For each number, the following checks are performed:
- If the number is a multiple of both 3 and 5 (i.e.,
i % 3 == 0 && i % 5 == 0
), the program prints “FizzBuzz”. - If the number is a multiple of 3 (i.e.,
i % 3 == 0
), the program prints “Fizz”. - If the number is a multiple of 5 (i.e.,
i % 5 == 0
), the program prints “Buzz”. - If the number is not a multiple of 3 or 5, the program prints the number itself.
This process continues until all numbers from 1 to 100 are processed and printed accordingly.