FizzBuzz Problem is a very easy problem , and also is asked in many interviews . FizzBuzz problem based on the Conditional Statement ( if, else, if-else ). If you solve the FizzBuzz problem then you are very good in Conditional Statement. If you are familiar with operator and conditional statement, So you can solve FizzBuzz problem easily. Let's start to discuss about What is FizzBuzz Problem ?
What is FizzBuzz Problem ?
A FizzBuzz problem is a very basic problem which is based on the conditional statement. FizzBuzz problem have some condition . let's see What are the condition in FizzBuzz Problem.
Conditions in FizzBuzz Problem
- If a Number is multiple of 3 and multiple of 5 , Then Print FizzBuzz .
- If a Number is multiple of 3 and Not the multiple of 5, Then Print Fizz.
- If a Number is multiple of 5 and Not the multiple of 3, Then Print Buzz.
- If a Number is Not the multiple of 3 and Not the multiple of 5, Then Print The Value of ( i ). Where i is a loop Variable.
This is some condition in FizzBuzz problem. You can write program in any programming language like C, Java, JavaScript, Python, C++, C# and so on.
There are various ways to solve this problem. And Some times interviewer asked you to solve this problem using Recursion. but in this post we are solving this problem using loop and conditional statement. We are going to write FizzBuzz Program in C, JavaScript, PHP and Java Programming Language. But first discuss how to approach FizzBuzz Problem.
FizzBuzz Problem Approach.
First we take an integer input from user . Create a for loop and initializing i=1 where i is a Loop variable. and set the condition i<=n, where n is a integer number inputted by User. Now , How we check if a Number is multiple of 3 and 5. We also Know if a number is Multiple of any number, then division of those number is always Zero ( 0 ). So we can check this condition by using Modulas( % ) Operator.
Example : If user enter 10 then loop executed 10 times , and every times loop checked four condition.
Condition First => if(i%3==0 && i%5==0)
Print => FizzBuzz
Condition Second => if(i%3==0 && i%5!=0)
Print => Fizz
Condition Third => if(i%3!=0 && i%5==0)
Print => Buzz
Condition Fourth => if(i%3!=0 && i%5!=0)
FizzBuzz Program in C
If you find the solution of FizzBuzz Problem in C programming Language. Then You can copy this code and run in Your C IDE, make sure a C compiler is installed in your system.
#include<stdio.h>
//Function Declareation
void FizzBuzz(int);
//Program Start Here
int main(){
int n;
printf("Enter a Number\n");
scanf("%d",&n);
//FizzBuzz Function Call Here
FizzBuzz(n);
}
//FizzBuzz Function Define Here
void FizzBuzz(int number){
int i;
for(i=1;i<=number;i++){
if(i%3==0 && i%5==0)
printf("FizzBuzz\n");
if(i%3==0 && i%5!=0)
printf("Fizz\n");
if(i%3!=0 && i%5==0)
printf("Buzz\n");
if(i%3!=0 && i%5!=0)
printf("%d\n",i);
}
}
FizzBuzz Program in JavaScript
If you find the solution of FizzBuzz Problem in JavaScript programming Language. Then You can copy this code and run in any browser you can also this Online HTML Code Editor
FizzBuzz Program in Java
If you find the solution of FizzBuzz Problem in Java programming Language. Then You can copy this code and run in Java IDE, make sure Jdk is installed in your system.
import java.util.Scanner;
class FizzBuzz {
public static void main(String[] arr) {
Scanner input= new Scanner(System.in);
int i,number;
System.out.println("Enter a Number");
number= input.nextInt();
for(i=1;i<=number;i++){
if(i%3==0 && i%5==0)
System.out.println("FizzBuzz");
if(i%3==0 && i%5!=0)
System.out.println("Fizz");
if(i%3!=0 && i%5==0)
System.out.println("Buzz");
if(i%3!=0 && i%5!=0)
System.out.println(i);
}
}
}
FizzBuzz Program in PHP
If you find the solution of FizzBuzz Problem in PHP programming Language. Then You can copy this code and run in any browser, make sure a server(Apache, Nginx) is installed in your system.
<?php
function FizzBuzz($number){
$i=0;
for($i=1;$i<=$number; $i++){
if($i%3==0 && $i%5==0)
echo "FizzBuzz\n";
if($i%3==0 && $i%5!=0)
echo "Fizz \n";
if($i%3!=0 && $i%5==0)
echo "Buzz\n";
if($i%3!=0 && $i%5!=0)
echo "$i \n";
}
}
FizzBuzz(15)
?>
Also Read :
Conclusion:
So We Discuss FizzBuzz Problem and also solve this problem in different programming language . If you have any question please contact us . If you feel this post is helpful then Keep Share..
0 Comments
Hey , Comment Your Query or Suggestion