Interview Programs

Fizz Buzz Program

22 Jul , 2015  

Fizz buzz program is one of the favorite question of interviewer to asked fresher when you attend the technical interview. Let’s first understand
what is the fizz buzz Program?

Question:”Write a program for a given natural number that prints the “fizz” if the number is dividable by 3 ,prints buzz” if the number is dividable by 5, prints “fizzbuzz” if the number is dividable by 15 otherwise the same number if no other requirement is fulfilled”

I think the fizz buzz Program question was not difficult but its new for fresher in simple words if the number is divisible by 3 then return “fizz” , if the number is divisible by 5 then return “buzz” , if the number is divisible by 15 then return “fizzbuzz”, otherwise return the same number

Solution:

import java.util.Scanner;
public class FizzBuzz {

	private static Scanner scanner = null;

	public static void main(String[] args) {
		scanner = new Scanner(System.in);
		System.out.println("Enter the input number");
		int inputNumber = scanner.nextInt(); //Get a input number from user
		System.out.println("Result :" + FizzBuzz.getResult(inputNumber));

	}

	public static String getResult(int number) {
		if (number % 15 == 0)
			return "fizzbuzz";
		if (number % 3 == 0)
			return "fizz";
		if (number % 5 == 0)
			return "buzz";
		return Integer.toString(number);
	}
}

If you have any doubts, any question regarding this fizz buzz Program Problem comments here. you can also share this problem on social sites by clicking on below links

,


4 Responses

  1. prasad says:

    nice explanation sir

  2. Raviraj says:

    Thank u so much..

  3. Kishor says:

    I am genuinely thankful to the holder of this website who has shared this impressive piece of writing at here.

Leave a Reply

Your email address will not be published. Required fields are marked *