FizzInterview.java

less than 1 minute read

This program was created to create a string where if it is a multiple of 3, it prints “Fizz” or when it is multiple of 5, it prints “Buzz”.

However, if it is a multiple of both 3 and 5, then “FizzBuzz” gets printed out.

If a number doesn’t match any of those conditions, then it prints itself.

The code has been modified slightly and now uploaded to the site.

<— Return Home

Code Snippet

/*
Compiler: JDK 11.0.11 LTS
Link: https://edabit.com/challenge/QCgoxbd32BqFr6AY7
*/
    
    
public class FizzInterview {
    public static void main(String[] args){
        String result = fizzBuzz(3);
        String result2 = fizzBuzz(5);
        String result3 = fizzBuzz(15);
        String result4 = fizzBuzz(4);
        System.out.println(result);
        System.out.println(result2);
        System.out.println(result3);
        System.out.println(result4);
    }
    public static String fizzBuzz(int n) {
        String answer1 = "Fizz";
        String answer2 = "Buzz";
        String answer3 = "FizzBuzz";
        if (n % 5 == 0 && n % 3 == 0){
            return answer3; 
        }
		else if (n % 3 == 0){
            return answer1;
        }
        else if (n % 5 == 0){
            return answer2;
        }
        else {
            String change = Integer.toString(n);
            return change;
        }
    }
}

<— Return Home