Firstly, when posting code, please enclose it in code tags to make it more readable.
The problem isn't really a C++ problem, it's a basic logic problem. You need to think more clearly about the logic of your
if and
else . For example, you have the following:
1 2 3 4 5 6 7 8 9
|
if (num % 2 == 0)
{
cout << "Divisible by 2!\n";
}
else if (num % 3 == 0)
{
cout << "Divisible by 3!\n";
}
|
This will only check whether num is divisible by 3 if it fails the test for being divisible by 2.
What will happen if
num is 6? The first test will register as true, as 6 is is divisible by 2. It will not perform the second test, so will not report that 6 is divisible by 3 as well.
The specific problem you mentioned is down to your final
if and
else:
1 2 3 4 5 6 7 8
|
if (num % 9 == 0)
{
cout << "Divisible by 9!\n";
}
else
{
cout << "Sorry not divisible by anything!!\n";
}
|
It should be clear what's happening here - if num isn't divisible by 9, then it will report "not divisible", regardless of what other numbers it is divisible by.