Problem Solving: The Fizz Buzz Game in Java | AskTheCode
Basic Java | Example of if - else in Java | Problem Solving in Java | Ask The Code
Problem:
The FizzBuzz Game Let's play a game of FizzBuzz! It works just like the popular childhood game "PopCorn", but with rules of math applied since math is fun, right? Just print out "Fizz" when the given number is divisible by 3, "Buzz" when it's divisible by 5, and "FizzBuzz" when it's divisible by both 3 and 5!
Let the game begin!
Input Sample:
5
Output Sample:
FizzBuzz
Code:
import java.util.Scanner;
public class solution_TheFizzBuzzGame{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n % 3 == 0 && n % 5 == 0) {
System.out.println("FizzBuzz");
}
else if (n % 3 == 0) {
System.out.println("Fizz");
}
else if (n % 5 == 0) {
System.out.println("Buzz");
}
}
}