-1

Everybody knows that FizzBuzz question that interviewers ask students.

Basically, when you have an incrementor and for each number which is a divisible of 3 you say fizz, for a number divisible by 5 you say buzz, while if it is divisible by both(3 and 5) you say FizzBuzz, hence the name.

It is a relatively easy problem to solve and I have done it, but I think my solution is a bit clunky. This is it:

int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
   numbers[i] = i;
}

for (int i : numbers) {
     if (i % 3 == 0) {
        System.out.println("Fizz");
     } else if(i % 5 == 0) {
         System.out.println("Buzz");
     } else {
         System.out.println("FizzBuzz");
     }
}

But the problem is that when the number is divisible by both 3 and 5 it gives me "Fizz" for some reason. Can somebody explain to me, because I'm new to java programming. Thanks in advance!

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • That's because the first condition will be `true` not regarding the second one. You should first check if it is divisible by both values, if not, check them separetely. – deHaar Jul 26 '19 at 07:40
  • 1
    That's a common error with FizzBuzz. If a number is divisible by 5 and 3, what does the first condition result in? – Federico klez Culloca Jul 26 '19 at 07:41
  • @deHaar giving away the solution like this doesn't help much when someone's learning, IMHO – Federico klez Culloca Jul 26 '19 at 07:41
  • 1
    Also, this prints fizzbuzz for all numbers divisible by neither 3 nor 5, e.g. 1, 2. – Andy Turner Jul 26 '19 at 07:42
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Jul 26 '19 at 07:43
  • @FedericoklezCulloca yeah, kinda right... I did't want to provide code, just the facts. If that's too much, I will have to write less comments. – deHaar Jul 26 '19 at 07:43
  • @FedericoklezCulloca I'm not convinced there is that much you can say without effectively "giving away" the solution. – Andy Turner Jul 26 '19 at 07:46
  • @AndyTurner, still IMHO, there's a difference between my comment and deHaar's one. But of course that's just my opinion. There's a reason I'm not downvoting the answers giving the full solution :) . But I still think OP would be better helped in the long term by a nudge in the right direction than by an outright solution. – Federico klez Culloca Jul 26 '19 at 07:50
  • Possible duplicate of [How to create the FizzBuzz using loops in JAVA](https://stackoverflow.com/questions/39652324/how-to-create-the-fizzbuzz-using-loops-in-java) – Gaurav Mall Aug 25 '19 at 07:43

3 Answers3

2

The problem lies in the order of your if statements. Lets take a look at the number 15, which is the first number divisible by both 3 and 5. Because of the order in which you have your if statements, the first statement that is checked is

if ( 15 % 3 == 0)

The result of the operation is indeed equal to 0, as 15 is divisible by 3 and so "Fizz" is printed and your else is ignored.

Think about how you should structure the order of your if statements and which additional condition should you introduce to catch the specific case of being divisible by both i % 3 == 0 && i % 5 == 0.

jure
  • 492
  • 5
  • 8
0

When you enter the if statement and your number is 15 for exemple, you enter the first if statement and.. prints "Fizz" as you stated, because 15 % 3 == 0 returns true. Then it ignores the else.

You want the first if to be

if (i % 3 == 0 && i % 5 == 0) {
  System.out.println("FizzBuzz");*
}
Félix
  • 388
  • 1
  • 9
0

Try this code

public static void main(String[] args) {
    int[] numbers = new int[100];
    for (int i = 0; i < numbers.length; i++) {
       numbers[i] = i;
    }

    for (int i : numbers) {
         if ((i % 3 == 0) && (i % 5 == 0)) {
            System.out.println("FizzBuzz");
         } else if(i % 5 == 0) {
             System.out.println("Buzz");
         } else if (i % 3 == 0){
             System.out.println("Fizz");
         }
    }
}
frianH
  • 7,295
  • 6
  • 20
  • 45