Fizz Buzz Program in C

In this tutorial, we will learn about Fizz Buzz Implementation in C programming. Let us start by understanding what Fizz Buzz is and its implementation in a program.

Fizz Buzz Program

A Fizz Buzz program prints the number from the range of 1 to n, where n is the input number taken from the user.

  • The program prints “FizzBuzz” if it is a multiple of 3 and 5.
  • The program prints “Fizz” if it is a multiple of only 3 but not 5.
  • The program prints “Buzz” is it is a multiple pf only 5 but not 3.
  • Lastly, it prints the number itself, if it is none of the above.

Example:

Input (n): 10
Output: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz

Now let us go through an example in the C program to implement FizzBuzz.


Fizz Buzz Program in C

//FizzBuzz program in C
#include <stdio.h>

int main()
{
   int i, n;

   printf("Enter the number: ");
   scanf("%d", &n);

   for (i = 1; i <= n; i++)
   {
      // For FizzBuzz (3 *5 =15)
      if (i % 15 == 0)
         printf("FizzBuzz\t");

      // For Fizz (3)
      else if ((i % 3) == 0)
         printf("Fizz\t");

      // For Buzz (5)
      else if ((i % 5) == 0)
         printf("Buzz\t");

      else	//if none of the above print n
         printf("%d\t", i);
   }

   return 0;
}

Output:

Enter the number: 20
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz