[Solved]: Hacker Rank FizzBuzz Problem

In this tutorial, we will look at the solution of hacker rank FizzBuzz Problem. This is a very basic problem that you will come across in hacker rank. We have covered the solution in four programming languages namely, Python3, C, Go and Java8.

 

[Solved]: Hacker Rank FizzBuzz Problem

Problem Statement 

Also read: 8 Things You Must Know in Python (2023)

Problem: FizzBuzz

Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows:

  • if i is a multiple of both 3 and 5, print FizzBuzz
  • if i is a multiple of 3(but not 5), print Fizz
  • if i is a multiple of 5(but not 3), print Buzz
  • if i is not a multiple of 3 or 5, print the value of i.

Constraints:

  • 0 <n< 2 x 1o^5

Sample Input

STDIN           Function
——–             ————–
15             ->           n = 15

Sample Output

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

 

[Solved]: Hacker Rank FizzBuzz Problem

We have provided the FizzBuzz problem solution in four programming languages. I will recommend you to try to convert the solution from one programming language to another by yourself before looking into the solution.

 

Python3 Solution

Using basic approach

#!/bin/python3

def fizzBuzz(n):

    for i in range(1, n+1):
        if i%3 == 0 and i%5 == 0:
            print("FizzBuzz")

        elif i%3 == 0:
            print("Fizz")

        elif i%5 == 0:
            print("Buzz")

        else:
            print(i)

if __name__ == '__main__':

    n = int(input().strip())
    fizzBuzz(n)

 

Using tuple

def fizzBuzz(n):

    fizz_buzz_mapping = [(3, "Fizz"), (5, "Buzz")]
    for i in range(1, n + 1):
        result = ''.join(word for num, word in fizz_buzz_mapping if i % num == 0)
        print(result or i)

if __name__ == '__main__':

    n = int(input().strip())
    fizzBuzz(n)

 

Go Solution

package main

import (
"fmt"
)

func fizzBuzz(n int) {

for i := 1; i <= n; i++ {
result := ""
if i%3 == 0 {
result += "Fizz"

}

if i%5 == 0 {
result += "Buzz"
}

if result == "" {
result = fmt.Sprint(i)
}

fmt.Println(result)
}
}

func main() {

var n int
fmt.Scan(&n)
fizzBuzz(n)
}

 

Java 8 Solution

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class FizzBuzz {
public static void main(String[] args) {
int n = Integer.parseInt(System.console().readLine("Enter a number: "));
fizzBuzz(n);
}

public static void fizzBuzz(int n) {
List<String> fizzBuzzMapping = new ArrayList<>();
fizzBuzzMapping.add("Fizz");
fizzBuzzMapping.add("Buzz");

IntStream.rangeClosed(1, n)
.mapToObj(i -> fizzBuzzMapping.stream()
.filter(pair -> i % Integer.parseInt(pair) == 0)
.collect(Collectors.joining()))
.forEach(result -> System.out.println(result.isEmpty() ? result : result));
}
}

 

C Solution

#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is divisible by another number
bool isDivisible(int num, int divisor) {
return num % divisor == 0;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

for (int i = 1; i <= n; i++) {
bool divisibleBy3 = isDivisible(i, 3);
bool divisibleBy5 = isDivisible(i, 5);

if (divisibleBy3 || divisibleBy5) {
if (divisibleBy3) {
printf("Fizz");
}
if (divisibleBy5) {
printf("Buzz");
}
} else {
printf("%d", i);
}

printf("\n");
}

return 0;
}

Leave a Comment