FizzBuzz Program in C is a simple programming task used in software developer job interviews. It is often used as an initial screening question to help filter out candidates who lack basic coding skills.
In this task, you are asked to write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz“. For numbers that are multiples of both three and five print “FizzBuzz“.
There have been many variations of this task like:
- FizzBuzzBang (multiples of 3, 5, and 7)
- FizzBuzzBangBong (multiples of 3, 5, 7, and 11)
- FizzBuzzBangBongBaz (multiples of 3, 5, 7, 11, and 13)
Example:
Multiples of 3 are printed as Fizz
Multiples of 5 are printed as Buzz
Multiples of 7 are printed as FizzBuzz
Remaining values print as Number
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
Write a FizzBuzz implementation in C. which will print the numbers 1 through 100. However, for multiples of three, write “Fizz” instead of the number, and for multiples of five, write “Buzz.” Print “FizzBuzz” for numbers that are multiples of three and five.
1. Iterate from 1 to 100.
2. If the number is divisible by both 3 and 5, print “FizzBuzz”.
3. Else if the number is divisible by 3, print “Fizz”.
4. Else if the number is divisible by 5, print “Buzz”.
5. Else print the number.
There are several ways to print FizzBuzz in C language. Let’s take a detailed look at all the approaches to print FizzBuzz in C.
- FizzBuzz Program in C using Simple Solution (Naive Approach)
- FizzBuzz Program in C using Optimized Solution
- FizzBuzz Program in C using Advanced Solution
In this approach, we will use a loop to iterate from 1 to 100 and check if the number is divisible by 3, 5, or both.
Here is source code of the C program to implement a FizzBuzz using naive approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to implement a FizzBuzz using naive approach
*/
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
printf("FizzBuzz ");
}
else if (i % 3 == 0)
{
printf("Fizz ");
}
else if (i % 5 == 0)
{
printf("Buzz ");
}
else
{
printf("%d ", i);
}
}
return 0;
}
1. In this program, we are using a for loop to iterate from 1 to 100.
2. After that use the modulus operator to check if the number is divisible by 3 or 5.
3. If the number is divisible by both 3 and 5, we are printing “FizzBuzz”.
4. If the number is divisible by 3, we are printing “Fizz”.
5. If the number is divisible by 5, we are printing “Buzz”.
6. If the number is not divisible by 3 or 5, we are printing the number.
Time complexity: O(n)
We are iterating from 1 to 100 and checking if the number is divisible by 3 or 5. So, n is 100. So, the time complexity of fizzbuzz program is O(100) which is O(n).
Space complexity: O(1)
We are not using any extra space. So, the space complexity of fizzbuzz program is O(1).
In this case, we are printing the numbers 1 through 100. For multiples of three, write “Fizz” instead of the number, and for multiples of five, write “Buzz.” Print “FizzBuzz” for numbers that are multiples of three and five.
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
In this approach we use flags to check if the number is divisible by 3, 5, or both instead of checking the number for divisibility every time as using the modulus operator is an expensive operation.
Here is source code of the C program to implement a FizzBuzz using optimized solution. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to implement a FizzBuzz using optimized solution
*/
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 50; i++)
{
int flag3 = i % 3 == 0;
int flag5 = i % 5 == 0;
if (flag3 && flag5)
{
printf("FizzBuzz ");
}
else if (flag3)
{
printf("Fizz ");
}
else if (flag5)
{
printf("Buzz ");
}
else
{
printf("%d ", i);
}
}
return 0;
}
1. In this approach, we use two flags initially to find whether the number is divisible by 3 or 5. This helps in decreasing the number of modulus operations.
2. After that check if the number is divisible by both 3 and 5, 3, or 5.
3. If the number is divisible by both 3 and 5, we are printing “FizzBuzz”.
4. If the number is divisible by 3, we are printing “Fizz”.
5. If the number is divisible by 5, we are printing “Buzz”.
6. If the number is not divisible by 3 or 5, we are printing the number.
Time complexity: O(n)
We are iterating from 1 to 100 and checking if the number is divisible by 3 or 5. So, n is 100. So, the time complexity of fizzbuzz program is O(100) which is O(n).
Space complexity: O(1)
We are not using any extra space. So, the space complexity of fizzbuzz program is O(1).
In this case, we are printing the numbers 1 through 100. For multiples of three, write “Fizz” instead of the number, and for multiples of five, write “Buzz.” Print “FizzBuzz” for numbers that are multiples of three and five.
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz
In this approach we’ll use a string to store the output and print it at the end of the loop.
Here is source code of the C program to implement a FizzBuzz using advanced solution. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to implement a FizzBuzz using String
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
for (i = 1; i <= 25; i++)
{
char output[10] = "";
if (i % 3 == 0)
{
strcat(output, "Fizz");
}
if (i % 5 == 0)
{
strcat(output, "Buzz");
}
if (strlen(output) == 0)
{
printf("%d ", i);
}
else
{
printf("%s ", output);
}
}
return 0;
}
1. In this approach, we are using a string to store the output.
2. We are using the modulus operator to check if the number is divisible by 3 or 5.
3. If the number is divisible by 3, we are appending “Fizz” to the string.
4. If the number is divisible by 5, we are appending “Buzz” to the string.
5. If the string is empty, we are printing the number. Else, we are printing the string.
Time complexity: O(n)
We are iterating from 1 to 100 and checking if the number is divisible by 3 or 5. So, n is 100. So, the time complexity of fizzbuzz program is O(100) which is O(n).
Space complexity: O(1)
We are not using any extra space. So, the space complexity of fizzbuzz program is O(1).
In this case, we are printing the numbers 1 through 100. For multiples of three, write “Fizz” instead of the number, and for multiples of five, write “Buzz.” Print “FizzBuzz” for numbers that are multiples of three and five.
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for C Internship
- Buy Computer Science Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Practice BCA MCQs