The FizzBuzz challenge is a classic challenge that’s used as an interview screening device for computer programmers. It’s a very simple programming task but it’s used to determine whether the job candidate can actually write code.
Sound fun and exciting? Let’s get started. In this article, you’ll learn how to solve the FizzBuzz challenge with implementations in 5 programming languages.
Problem Statement
You need to write a program that prints the numbers from 1 to 100 such that:
- If the number is a multiple of 3, you need to print "Fizz" instead of that number.
- If the number is a multiple of 5, you need to print "Buzz" instead of that number.
- If the number is a multiple of both 3 and 5, you need to print "FizzBuzz" instead of that number.
Try to think of a solution to solve this challenge with the help of loops and conditional statements before moving to the solution.
Approach to Solve the FizzBuzz Challenge
You need to follow the approach below to solve this challenge:
- Run a loop from 1 to 100.
- Numbers that are divisible by 3 and 5 are always divisible by 15. Therefore check the condition if a number is divisible by 15. If the number is divisible by 15, print "FizzBuzz".
- Check the condition if a number is divisible by 3. If the number is divisible by 3, print "Fizz".
- Check the condition if a number is divisible by 5. If the number is divisible by 5, print "Buzz".
Note: You can check if a number is divisible by another number using the modulo operator (%). For example: 25 % 5 == 0, therefore 25 is divisible by 5.
Pseudocode for the FizzBuzz Challenge
Below is the pseudocode for the FizzBuzz challenge:
for number from 1 to 100:
if (number is divisible by 3 and 5) then:
print("FizzBuzz")
if (number is divisible by 3) then:
print("Fizz")
if (number is divisible by 5) then:
print("Buzz")
Related: What Is Coding and How Does It Work?
C++ Program to Solve the FizzBuzz Challenge
Below is the C++ program to solve the FizzBuzz challenge:
// C++ program to implement the FizzBuzz problem
#include <iostream>
using namespace std;
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0)
{
cout << "FizzBuzz" << " ";
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
cout << "Fizz" << " ";
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
cout << "Buzz" << " ";
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
cout << i << " ";
}
}
return 0;
}
Output:
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
Related: How to Learn C++ Programming: Best Sites to Get Started
Python Program to Solve the FizzBuzz Challenge
Below is the Python program to solve the FizzBuzz challenge:
# Python program to implement the FizzBuzz problem
for i in range(1, 101):
# Numbers that are divisible by 3 and 5
# are always divisible by 15
# Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0):
print("FizzBuzz", end=" ")
# "Fizz" is printed in place of numbers
# that are divisible by 3
elif (i%3 == 0):
print("Fizz", end=" ")
# "Buzz" is printed in place of numbers
# that are divisible by 5
elif(i%5 == 0):
print("Buzz", end=" ")
# If none of the above conditions are satisfied,
# the number is printed
else:
print(i, end=" ")
Output:
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
Related: How to Get Started With Python Using a "Hello World" Script
JavaScript Program to Solve the FizzBuzz Challenge
Below is the JavaScript program to solve the FizzBuzz challenge:
// JavaScript program to implement the FizzBuzz problem
for (let i=1; i<=100; i++) {
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0) {
document.write("FizzBuzz" + " ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0) {
document.write("Fizz" + " ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0) {
document.write("Buzz" + " ");
}
// If none of the above conditions are satisfied,
// the number is printed
else {
document.write(i + " ");
}
}
Output:
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
Related: The Best Beginner Projects for New Programmers
Java Program to Solve the FizzBuzz Challenge
Below is the Java program to solve the FizzBuzz challenge:
// Java program to implement the FizzBuzz problem
public class Main
{
public static void main(String args[])
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15==0)
{
System.out.print("FizzBuzz"+" ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if (i%3==0)
{
System.out.print("Fizz"+" ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if (i%5==0)
{
System.out.print("Buzz"+" ");
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
System.out.print(i+" ");
}
}
}
}
Output:
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
C Program to Solve the FizzBuzz Challenge
Below is the C program to solve the FizzBuzz challenge:
// C program to implement the FizzBuzz problem
#include <stdio.h>
int main()
{
for (int i=1; i<=100; i++)
{
// Numbers that are divisible by 3 and 5
// are always divisible by 15
// Therefore, "FizzBuzz" is printed in place of that number
if (i%15 == 0)
{
printf("FizzBuzz ");
}
// "Fizz" is printed in place of numbers
// that are divisible by 3
else if ((i%3) == 0)
{
printf("Fizz ");
}
// "Buzz" is printed in place of numbers
// that are divisible by 5
else if ((i%5) == 0)
{
printf("Buzz ");
}
// If none of the above conditions are satisfied,
// the number is printed
else
{
printf("%d ", i);
}
}
return 0;
}
Output:
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
Start Your Coding Journey With a "Hello, World!" Program
The "Hello, World!" program is the first step for programmers to become acquainted with a new programming language. It’s considered to be one of the simplest programs possible in almost all languages.
If you’re a newbie to the programming world and exploring different languages, the "Hello, World!" program is the best choice to get started with a new programming language.
MUO – Feed