# A Multiple Of 3 or 5?

- by Joe Jr Yamut

Saw this problem on the Internet about Multiples. Thought I might solve it. Don’t know why I dislike these kinds of things, but still want to try to solve it. It always seems tricky, although elementary?

For transparency I only reviewed the *Maths* definition of a *Multiple* just to make sure if memory has not failed me yet. I promise. Scouts honor. 🖖 And on how to determine if bigger numbers are multiples of 3 or 5 at the later part of this writing, reviewed those *after* writing my short piece of Java code below.

What is a Multiple in Math? A multiple in math are the numbers you get when you multiply a certain number by an integer. For example, multiples of 7 are: 14, 21, 28, 35, 42, 49…etc.

Credit: https://happynumbers.com/blog/what-is-a-multiple-in-math/

Problem:

Is the integerna multiple of 3 and/or 5

Output:

Ifnis a multiple of both 3 and 5, print FooBar. Ifnis a multiple of 3 but not 5, print Foo. Ifnis a multiple of 5 but not 3, print Bar. Ifnis neither a multiple of 3 and 5, printn.

My solution:

No idea how I came up with this answer. I’ve always considered myself poor in Maths. The first thing that came to my mind was – *use modulo operator to solve this problem, Jose*. Hahaha! 😅 Also, I just focused on the integers 3 and 5. Nothing else.

We know that multiples of 3 are: 3, 6, 9, 10, 12, 15 and so on…

While multiples of 5 are: 5, 10, 15, 20, 25 and so forth…

So *n* divided by 3 and 5 should result in a whole number. It can’t be a fraction. This should be enough to print the correct output.

Code follows:

public static void fooBar(int n) {

int multipleOfThree = n % 3;

int multipleOfFive = n % 5;

if (multipleOfThree == 0 && multipleOfFive == 0) {

System.out.println("FooBar");

return;

}

if (multipleOfThree == 0 && multipleOfFive != 0) {

System.out.println("Foo");

return;

}

if (multipleOfThree != 0 && multipleOfFive == 0) {

System.out.println("Bar");

return;

}

System.out.println(n);

}

Result after running it with integers 1 to 15:

```
1
2
Foo
4
Bar
Foo
7
8
Foo
Bar
11
Foo
13
14
FooBar
```

The first test above looks to be correct. Would it still be true for higher numbers? Let’s test it out with higher numbers, why not.

A test for 10 random numbers between 100 and 2000 (inclusive).

Generated random number: 602 602 Generated random number: 1742 1742 Generated random number: 1898 1898 Generated random number: 475 Bar Generated random number: 1356 Foo Generated random number: 1243 1243 Generated random number: 1980 FooBar Generated random number: 694 694 Generated random number: 1649 1649 Generated random number: 869 869

To check if *n* is a multiple of 3. One trick I found out was to add each of the digits and the resulting sum should be divisible by 3.

How about for 5? This one is easier, since any whole number that ends in a zero or 5 is always divisible by 5.

So *1 + 3 + 5 + 6 = 15*. And 15 is obviously divisible by 3.

*475* on the other hand ends in 5. That checks out for the divisible by 5 rule. While *4 + 7 + 5 = 16*. Not divisible by 3.

Now *1 + 9 + 8 + 0 = 18*. And it also ends in zero. This number is a multiple of both 3 and 5.

Sum of *1 + 6 + 4 + 9 = 20*. That’s shy of 21. Not divisible by 3. Definitely not with 5.

Looks like the solution works. Can this be written in a shorter way? How about with lesser or no *if* statements? Will it work for other numbers? Let me think about that later. At least I know it is safe from division by zero. You can pass zero to *fooBar(n)* and it should print out “*FooBar*“.

Wait what? I also wondered about this too. Is the answer correct? Well, as it turns out zero is a multiple of 3 and 5, or any other whole number, other than itself. Surely, where have I been in school when all these were being discussed by the teacher? 😝

### Similar Posts:

- > Solving arrays – problem #1: Closest to zero July 30, 2010
- > Rewriting Nested If/Else September 7, 2022
- > A Theory On The RNG Factor In WOW May 22, 2009
- > Assigning Numbers to Groups – Problem #2: Distribute 1 Per Group or Even/Odd November 4, 2020
- > the number 11 July 18, 2006

Saw this problem on the Internet about Multiples. Thought I might solve it. Don’t know why I dislike these kinds of things, but still want to try to solve it. It always seems tricky, although elementary? For transparency I only reviewed the Maths definition of a Multiple just to make sure if memory has not…