Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Fizz Buzz problem solution

YASH PAL, 31 July 2024

In this Leetcode Fizz Buzz problem solution we have given an integer n, return a string array answer (1-indexed) where:

  1. answer[i] == “FizzBuzz” if i is divisible by 3 and 5.
  2. answer[i] == “Fizz” if i is divisible by 3.
  3. answer[i] == “Buzz” if i is divisible by 5.
  4. answer[i] == i if non of the above conditions are true.
Leetcode Fizz Buzz problem solution

Problem solution in Python.

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        
        final_list = []
        for i in range(1, n+1):
            
            if i%15 == 0:
                final_list.append("FizzBuzz")
            elif i%5 == 0:
                final_list.append("Buzz")   
            elif i%3 == 0:
                final_list.append("Fizz")
            else:
                final_list.append(str(i))
                
        return final_list

Problem solution in Java.

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> result = new ArrayList<String>(); 
        for(int i=1;i<=n;i++){
            if(i%3==0 && i%5==0){
                result.add("FizzBuzz");
                continue;
            }
            
            if(i%3==0){
                result.add("Fizz");
                continue;
            }
            
            if(i%5==0){
                result.add("Buzz");
                continue;
            }
            
            result.add(i+"");
        }
        
        return result;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string>ans;
        for(int i=1;i<=n;i++)
        {
            string ap;
            if(i%3==0)
                ap+="Fizz";
            if(i%5==0)
                ap+="Buzz";
            if(ap.length()==0)
                ap=to_string(i);
            ans.push_back(ap);
                
        }
        return ans;
    }
};

Problem solution in C.

char ** fizzBuzz(int n, int* returnSize){

char **res=(char**)calloc(n,sizeof(char *));int c=0;
for(int i=1;i<=n;i++)
{
    if(i%3==0&&i%5==0)
    {
        *(res+c)=(char *)calloc(9,sizeof(char));
        res[c][0]='F';res[c][1]='i';res[c][2]='z';
        res[c][3]='z';res[c][4]='B';res[c][5]='u';
        res[c][6]='z';res[c][7]='z';res[c][8]='';
        c++;
    }
    else if(i%3==0)
    {
        *(res+c)=(char *)calloc(5,sizeof(char));
        res[c][0]='F';res[c][1]='i';res[c][2]='z';
        res[c][3]='z';res[c][4]='';
        c++;
    }
    else if(i%5==0)
    {
        *(res+c)=(char *)calloc(5,sizeof(char));
        res[c][0]='B';res[c][1]='u';res[c][2]='z';
        res[c][3]='z';res[c][4]='';
        c++;
    }
    else
    {
        int x=0,y=i;
        while(y)
        {
            x++;
            y=y/10;
        }
        *(res+c)=(char *)calloc(x+1,sizeof(char));
        res[c][x]='';
        x--;
        y=i;
        while(y)
        {
            int m=y%10;
            res[c][x--]=48+m;
            y=y/10;
        }
        c++;
    }
    
}
*returnSize=c;
return res;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes