Fizzbuzz Program in C

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

Problem Description

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.

Problem Solution

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.

advertisement
advertisement

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.

Method 1: FizzBuzz Program in C using Simple Solution (Naive Approach)

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.

Program/Source Code

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /* 
  2.  * C Program to implement a FizzBuzz using naive approach
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int i;
  10.     for (i = 1; i <= 100; i++)
  11.     {
  12.         if (i % 3 == 0 && i % 5 == 0)
  13.         {
  14.             printf("FizzBuzz ");
  15.         }
  16.         else if (i % 3 == 0)
  17.         {
  18.             printf("Fizz ");
  19.         }
  20.         else if (i % 5 == 0)
  21.         {
  22.             printf("Buzz ");
  23.         }
  24.         else
  25.         {
  26.             printf("%d ", i);
  27.         }
  28.     }
  29.     return 0;
  30. }
Program Explanation

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).

Runtime Test Cases

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.

advertisement
 
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

Method 2: FizzBuzz Program in C using Optimized Solution

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.

Program/Source Code

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.

  1. /* 
  2.  * C Program to implement a FizzBuzz using optimized solution
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int i;
  10.     for (i = 1; i <= 50; i++)
  11.     {
  12.         int flag3 = i % 3 == 0;
  13.         int flag5 = i % 5 == 0;
  14.         if (flag3 && flag5)
  15.         {
  16.             printf("FizzBuzz ");
  17.         }
  18.         else if (flag3)
  19.         {
  20.             printf("Fizz ");
  21.         }
  22.         else if (flag5)
  23.         {
  24.             printf("Buzz ");
  25.         }
  26.         else
  27.         {
  28.             printf("%d ", i);
  29.         }
  30.     }
  31.     return 0;
  32. }
Program Explanation

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.

advertisement

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).

Runtime Test Cases

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

Method 3: FizzBuzz implementation in C using Advanced Solution

In this approach we’ll use a string to store the output and print it at the end of the loop.

Program/Source Code

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.

  1. /* 
  2.  * C Program to implement a FizzBuzz using String
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. int main(void)
  9. {
  10.     int i;
  11.     for (i = 1; i <= 25; i++)
  12.     {
  13.         char output[10] = "";
  14.         if (i % 3 == 0)
  15.         {
  16.             strcat(output, "Fizz");
  17.         }
  18.         if (i % 5 == 0)
  19.         {
  20.             strcat(output, "Buzz");
  21.         }
  22.         if (strlen(output) == 0)
  23.         {
  24.             printf("%d ", i);
  25.         }
  26.         else
  27.         {
  28.             printf("%s ", output);
  29.         }
  30.     }
  31.     return 0;
  32. }
Program Explanation

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).

Runtime Test Cases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.