The Fizz Buzz problem is a common programming challenge frequently used in coding interviews to assess problem-solving skills. The task is simple: you need to write a program that prints the numbers from 1 up to a specified limit.
The output must follow these rules:
- For numbers divisible by 3, print "Fizz" in place of the number.
- For numbers divisible by 5, print "Buzz" in place of the number.
- For numbers divisible by both 3 and 5, print "FizzBuzz" in place of the number.
- For all other numbers, print the number itself.
This tutorial will walk you through implementing the Fizz Buzz program in C.
Why is Fizz Buzz Important?
The Fizz Buzz problem is an excellent way to practice using loops and conditionals. It helps reinforce basic programming skills such as handling control flow and logical expressions. Fizz Buzz is also a common question in coding interviews, testing how effectively a programmer can implement simple logic in code.
How to Write a Fizz Buzz Program in C
To implement Fizz Buzz, we will loop through a series of numbers and use if-else statements to check the divisibility of each number by 3, 5, or both. Depending on the result of these conditions, the appropriate output will be printed.
Example:
#include <stdio.h>
int main() {
int i, n;
// Ask the user for the limit
printf("Enter the limit: ");
scanf("%d", &n);
// Loop from 1 to the entered limit
for(i = 1; i <= n; i++) {
// If the number is divisible by both 3 and 5, print "FizzBuzz"
if (i % 3 == 0 && i % 5 == 0) {
printf("FizzBuzz\n");
}
// If the number is divisible by 3, print "Fizz"
else if (i % 3 == 0) {
printf("Fizz\n");
}
// If the number is divisible by 5, print "Buzz"
else if (i % 5 == 0) {
printf("Buzz\n");
}
// If none of the above, just print the number
else {
printf("%d\n", i);
}
}
return 0;
}
Explanation:
- Input: The program asks the user to input a limit (
n
), which is the number up to which the program will print. - Loop: The program loops through each number from 1 to
n
. - Conditionals:
- It checks if the number is divisible by both 3 and 5 first. If true, it prints "FizzBuzz".
- If not, it checks if the number is divisible by 3. If so, it prints "Fizz".
- Next, it checks if the number is divisible by 5 and prints "Buzz".
- If none of these conditions are met, it simply prints the number.
Sample Output:
If you run the program with a limit of 15, the output will be:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Understanding the Output
- For numbers like 3 and 6, "Fizz" is printed because they are divisible by 3.
- For numbers like 5 and 10, "Buzz" is printed because they are divisible by 5.
- For number 15, "FizzBuzz" is printed because it is divisible by both 3 and 5.
- Numbers not divisible by either 3 or 5 are printed as they are.
Conclusion
This tutorial explained how to write a C program to solve the Fizz Buzz problem. By using simple loops and conditional statements, we created a program that checks the divisibility of numbers and prints the appropriate result. This exercise is a helpful way to practice fundamental programming concepts such as iteration and conditional logic.