# Exciting FizzBuzz Challenge in Python With Solution

There are thousands of python learning platform where you can practice your Python coding skills. These platforms contain some of the best problems which you can ever imagine. The programs are separated into several categories depending on their topic category and difficulty level. These platforms definitely help you learn new things and improve your coding practices. In this post, we’ll go through the solutions of FizzBuzz Python.

FizzBuzz Python is a popular python question in HackerRank and HackerEarth learning platforms. Both the platforms have the same problem statement and are very special for new programmers. The program asks you to print “Fizz” for the multiple of 3, “Buzz” for the multiple of 5, and “FizzBuzz” for the multiple of both. In both the platforms, the best optimal solution for the program is expected, which takes the lowest time to execute.

In this post, we’ll go through all of the solutions in all languages, including python 2 and python 3.

Contents of Tutorial

## What exactly is the FizzBuzz Python Problem Statement?

The exact wordings of the problem goes as –

Print every number from 1 to 100 (both included) on a new line. Numbers which are multiple of 3, print “Fizz” instead of a number. For the numbers which are multiples of 5, print “Buzz” instead of a number. For the number which is multiple of both 3 and 5, print “FizzBuzz” instead of numbers.

Problem statement seems very easy for an everyday programmer. But from a newbie’s perspective, this program tests the skills regarding loops and conditionals. Let’s have a look at the constraints given for the answers to be acceptable.

## Constraints for the FizzBuzz Problem

Constraints are the limiting factors within which your code must comply. These constraints are made to identify better codes with minimum time complexity and better memory management. Following are the constraints for the FizzBuzz Python problem –

1. Time Limit: 5 seconds
2. Memory Limit: 256 MB
3. Source Limit: 1024KB
4. Scoring System: (200 – number of characters in source code)/100 [Only for python solutions]

## Hints For FizzBuzz Python Problem

There are multiple ways to solve the FizzBuzz Python problem. If you want hints for the same here, they are –

Hint 1: Create a “for” loop with range() function to create a loop of all numbers from 1 to 100. Before implementing FizzBuzz, create this simple loop to understand the looping.

Hint 2: To check the number is a multiple of any number, check the remainder of the number with the divisor. If the remainder turns out to be 0, then it’s multiple of the corresponding number. For example, 15 leaves remainder 0 when divided by 5. This confirms that 15 is a multiple of 5. Use the same logic to create a logical conditional.

Hint 3: In conditional statements, put the multiple of 15 cases on top of 5 or 3. Because if the number is a multiple of 15, it’ll always be a multiple of 3 and 5. Implementing this will check for the FizzBuzz case first.

## FizzBuzz Python 3 Solution

Solution for FizzBuzz problem in Python 3 –

```for num in range(1, 101):
if num % 15 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
else:
print(num)
```

Output –

Explanation –

Firstly, we declare a loop that ranges from 1 to 100. As the range() function loops till inclusive integer, we’ve used 101. We’ve used the if statements from the next block to check if the multiplicity of every number. If it is divisible by 15, print “FizzBuzz,” if it’s divisible by 3, print “Fizz” if it’s divisible by 5, print “Buzz.” All these conditionals are combined by using if and elif blocks. This looping goes on until it reaches 100.

## FizzBuzz Python 2 Solution

Solution for FizzBuzz problem in Python 2 –

```for num in range(1, 101):
if num % 15 == 0:
print "FizzBuzz"
elif num % 3 == 0:
print "Fizz"
elif num % 5 == 0:
print "Buzz"
else:
print num
```

Explanation –

Explanation follows the same for python 2. The only difference being that the print function works without parenthesis.

## Fizzbuzz Python One Liner Solution

Code:

```for i in range(1, 101): print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i))
```

Explanation:

Python supports one-liner for loops included with conditional statements. FizzBuzz is a perfect problem where you can code the entire solution in one line. Using loops and conditionals in one line, you can score maximum points.

## Solutions for FizzBuzz in Other Languages

### Solving FizzBuzz Problem In C++

```#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=100;i++){
if((i%3 == 0) &amp;&amp; (i%5==0))
cout<<"FizzBuzz\n";
else if(i%3 == 0)
cout<<"Fizz\n";
else if(i%5 == 0)
cout<<"Buzz\n";
else
cout<<i<<"\n";
}
return 0;
}
```

### Solving FizzBuzz Problem in Java 8

```import java.io.*;
import java.util.*;

public class Solution {
public static void main(String[] args) {
int x = 100;
for(int i = 1; i <= x; i++){
if(i % 3 == 0 &amp;&amp; i % 5 ==0){
System.out.println("FizzBuzz");
}
else if(i % 5 == 0){
System.out.println("Buzz");
}
else if(i % 3 ==0){
System.out.println("Fizz");
}
else{
System.out.println(i);
}
}
}
}
```

### FizzBuzz Problem In Go

```package main

import "fmt"

func main() {

for i := 1; i <= 100; i++ {
if i%15==0 {
fmt.Printf("FizzBuzz\n")
} else if i%3 == 0 {
fmt.Printf("Fizz\n")
} else if i%5 == 0 {
fmt.Printf("Buzz\n")
} else {
fmt.Printf("%d\n", i)
}
}
}
```

### Solving FizzBuzz Problem In Javascript (NodeJS v10)

```process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});

process.stdin.on("end", function () {
main(stdin_input);
});
function main(input) {
var str;
var i=1;
while(i<=input){
str='';
if(i%3===0){
str+='Fizz';
}
if(i%5===0){
str+='Buzz';
}
str!=='' ? process.stdout.write(str+"\n") : process.stdout.write(i+"\n");
i++;
}
}
```

### Solving FizzBuzz Problem In PHP

```<?php
for (\$i = 1; \$i <= 100; \$i++) {
if ((\$i % 3) == 0)
echo "Fizz";
if ((\$i % 5) == 0)
echo "Buzz";
if ((\$i % 3) != 0 &amp;&amp;  (\$i % 5) != 0)
echo \$i;
echo "\n";
}
?>
```

### R (RScript 3.4.0)

```for (i in 1:100){
if (i%%3 == 0)
if (i%%5 == 0)
cat("FizzBuzz\n")
else
cat("Fizz\n")
else
if (i%%5 == 0)
cat("Buzz\n")
else
cat(paste(i,"\n"))
}
```

## Conclusion

There are thousands of awesome problems that test your basic knowledge in the world of coding. These problems not only help you to learn to code but also improves your logical thinking. Hence, you should always practice coding problems even if you are in a job. There is no harm in learning more everything. To summarize, the FizzBuzz problem tests your basic coding knowledge.

Enjoy Learning and Enjoy Coding!

5 1 vote
Article Rating
Subscribe
Notify of