How I Solved the FizzBuzz Problem with a Switch Case in Java

Solve the classic FizzBuzz problem using a switch case in Java. The FizzBuzz problem is a common coding challenge that asks you to print numbers from 1 to a given number, but for multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz," and for numbers which are multiples of both 3 and 5, print "FizzBuzz.

Algogenz logo

4m · 4min read

Hey everyone! Today, I want to share how I solved the classic FizzBuzz problem using a switch case in Java. The FizzBuzz problem is a common coding challenge that asks you to print numbers from 1 to a given number, but for multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz," and for numbers which are multiples of both 3 and 5, print "FizzBuzz." Let's dive into how I tackled this using a switch case.


The Code

Here is the complete code for solving the FizzBuzz problem using a switch case:

import java.util.Scanner;

public class FizzBuzzSwitch {
    public static void main(String[] args) {
        int number;
        String result = "";

        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);

        System.out.print("Hey! Enter a number: ");
        number = input.nextInt();

        boolean divisibleBy5 = number % 5 == 0;
        boolean divisibleBy3 = number % 3 == 0;

        int switchCase = (divisibleBy3 ? 1 : 0) + (divisibleBy5 ? 2 : 0);

        switch (switchCase) {
        case 1:
            result = "Fizz";
            break;
        case 2:
            result = "Buzz";
            break;
        case 3:
            result = "FizzBuzz";
            break;
        default:
            result = String.valueOf(number);
            break;
        }

        System.out.println(result);
    }
}

Breaking Down the Code

Let me break down the code step-by-step to explain how it works.


Setting Up the Variables

int number;
String result = "";
  • number will store the input number that we will get from the user.
  • result will store the final result, which we will print out at the end.


Getting User Input

@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);

System.out.print("Hey! Enter a number: ");
number = input.nextInt();
  • We use a Scanner object to get user input from the console.
  • @SuppressWarnings("resource") is an annotation that suppresses the warning that the Scanner object is not closed. In a small program like this, it's okay to leave it open.
  • We prompt the user to enter a number and read that number using input.nextInt(), which stores the value in the number variable.


Checking Divisibility

boolean divisibleBy5 = number % 5 == 0;
boolean divisibleBy3 = number % 3 == 0;
  • We use the modulo operator % to check if the number is divisible by 5 and 3.
  • number % 5 == 0 evaluates to true if the number is divisible by 5, and false otherwise.
  • number % 3 == 0 evaluates to true if the number is divisible by 3, and false otherwise.
  • We store these boolean results in the variables divisibleBy5 and divisibleBy3.


Calculating the Switch Case Value

int switchCase = (divisibleBy3 ? 1 : 0) + (divisibleBy5 ? 2 : 0);
  • We use the ternary operator ? : to determine the values based on the boolean variables divisibleBy3 and divisibleBy5.
  • (divisibleBy3 ? 1 : 0) evaluates to 1 if divisibleBy3 is true and 0 if false.
  • (divisibleBy5 ? 2 : 0) evaluates to 2 if divisibleBy5 is true and 0 if false.


By adding these two values, switchCase can be 0, 1, 2, or 3, which represents the following cases:

  • 0: The number is not divisible by 3 or 5.
  • 1: The number is divisible by 3 but not by 5.
  • 2: The number is divisible by 5 but not by 3.
  • 3: The number is divisible by both 3 and 5.


Using the Switch Case

switch (switchCase) {
case 1:
    result = "Fizz";
    break;
case 2:
    result = "Buzz";
    break;
case 3:
    result = "FizzBuzz";
    break;
default:
    result = String.valueOf(number);
    break;
}

We use a switch case statement to determine the output based on the value of switchCase.

  • case 1: means the number is divisible by 3, so we set result to "Fizz".
  • case 2: means the number is divisible by 5, so we set result to "Buzz".
  • case 3: means the number is divisible by both 3 and 5, so we set result to "FizzBuzz".
  • default: handles the case where the number is not divisible by 3 or 5, so we set result to the number itself as a string using String.valueOf(number).


Printing the Result

System.out.println(result);

Finally, we print the result to the console.


So, using a switch case to solve the FizzBuzz problem in Java is an efficient and clean approach. It allows us to handle different cases of divisibility in a straightforward manner. This method ensures that the FizzBuzz logic is easy to understand and maintain. Give it a try and see how it works for you! Happy coding!

Related Tags

Recommended