if/elseif/else % division program problem

Im having a problem with the evenly divisible by both
3 and 5, display "WooHoo part. if I enter a 9 it will display woo and a number evenly by 5 will display hoo but the "by both" such as 15 will only display woo I think my if (num1 % 3 == 0 && num1 % 5 == 0)
cout << "WOO HOO.\n"; is the problem any help would be appreciated. this is part of a switch program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 2. For the second choice, ask the user for an integer.If the integer is evenly divisible by 3,
		display "Woo". If the number is evenly divisible by 5, display "Hoo". If it is evenly divisible by both
		3 and 5, display "WooHoo".Display "Sorry" otherwise. Implement this using if/elseif/else.
		(Reminder: Use the modulus operator (%) to check if an integer is divisible by a given number)break*/

	case 2:
		cout << "Please enter a number \n";
		cin >> num1;
		if (num1 % 3 == 0)
		{
			cout << " WOO.\n";
		}
		else
		{
			if (num1 % 5 == 0)
			{
				cout << " HOO.\n";
			}
			else
			{
				if (num1 % 3 == 0 && num1 % 5 == 0)
				{
					cout << "WOO HOO.\n";
				}
				else
				{
					cout << "Sorry" << endl << endl;
				}
  
It's because when the number is both evenly divisible by 3 and 5, both the conditions that you've written are satisfied.
(1) (num1 % 3 == 0)
(2) (num1 % 3 == 0 && num1 % 5 == 0)

Since (1) was evaluated before (2), the block of (1) is executed.
So you could put (2) to be tested before the other two tests,

or you could also do something like this:
1
2
3
4
5
6
7
8
9
10
11
	case 2:
		cout << "Please enter a number \n";
		cin >> num1;
		if (num1 % 3 == 0)
		{
			cout << " WOO";
		}
		if (num1 % 5 == 0)
		{
			cout << " HOO";
		}
I think if (num1 % 3 == 0 && num1 % 5 == 0) should be the first thing you check for, rather than the last.
closed account (z05DSL3A)
Take a look at:
https://www.youtube.com/watch?v=QPZ0pIK_wsc
@Alb13G

Remember what else does? When an if can be used, the else parts are ignored. So, when line 9 is true, the rest of the checking is not done. If line 9 is false, meaning the number IS NOT divisible by 3, then line 15 is checked. If line 15 is true, as in the 15 input, the rest of the checking is not done, so, you do not get, "WOO HOO."

You need more explicit checking, as in..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* 2. For the second choice, ask the user for an integer.If the integer is evenly divisible by 3,
	display "Woo". If the number is evenly divisible by 5, display "Hoo". If it is evenly divisible by both
	3 and 5, display "WooHoo".Display "Sorry" otherwise. Implement this using if/elseif/else.
	(Reminder: Use the modulus operator (%) to check if an integer is divisible by a given number)break*/

	case 2:
		{
			cout << "Please enter a number \n";
			cin >> num1;
			if (num1 % 3 == 0 && num1 % 5 != 0)
			{
				cout << " WOO.\n";
			}
			else if (num1 % 3 != 0 && num1 % 5 == 0)
			{
				cout << " HOO.\n";
			}
			else if (num1 % 3 == 0 && num1 % 5 == 0)
			{

				cout << "WOO HOO.\n";
			}
			else
			{
				cout << "Sorry." << endl << endl;
			}
		}
		break;
Last edited on
Thanks Guys I put it first I appreciate your help ! Cool video Grey Wolf It reminded me about my () :) Im off to case 3 where I have to write case 2 with a conditional operator ill prob need some help thanks again
isnt %15 the same as divisible by BOTH 3 & 5?
Yes.
Does "evenly divisible" mean the same as "divisible" or am I missing something?
Yes, same thing. Just redundant to differentiate from "able to be divided, without a remainder".
Last edited on
technically divisible just means able to be divided by, the 'evenly' is implied here by common usage & context. If division by zero or something were part of the problem, it would be better to be more careful with the words. (And my question was not really a question, so much as trying to get the OP to think that direction). For extra credit, try to do the %s only once and see how few comparisons you really need.

also the code seems bugged?
due to else-if logic, it will NEVER print woohoo.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string answer[] = { "Sorry", "Woo", "Hoo", "WooHoo" };
   int N;
   cout << "Input a number: ";   cin >> N;
   cout << answer[ !(N%3) + 2 * !(N%5) ] << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

using namespace std;

int
main()
{
    for (int n=0 ;n < 20; ++n) {
	cout << n << ": ";
	if (n % 3 == 0 && n % 5 == 0) {
	    cout << "WOO HOO\n";
	} else if (n % 3 == 0) {
	    cout << "WOO\n";
	} else if (n % 5 == 0) {
	    cout << "HOO\n";
	} else {
	    cout << "Sorry\n";
	}
    }
    return 0;
}


But I like lastchance's solution best. If !(N%3) + 2 * !(N%5) is mysterious, here's an explanation. It creates a two-bit number. The high order bit is set if N%5 is 0. The low order bit is set if N%3 is 0. The resulting number then gives the index into the array.
lookup tables solve so many annoying problems very efficiently (easy to code, and fast to execute). OP, learn this technique... its one of the few where doing less work gives a better result consistently.
Topic archived. No new replies allowed.