When I type 64 for input I get the correct results expect for the last else, ("Sorry not divisible by anything!") I get it every time! I do not know how to make it work, can anyone please help me I am really struggling and I am a freshmen in college in my first programing class. Thanks I would appreciate it.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Please enter a number\n";
cin >> num;
if (num % 2 == 0)
{
cout << "Divisible by 2!\n";
}
if (num % 3 == 0)
{
cout << "Divisible by 3!\n";
}
if (num % 4 == 0)
{
cout << "Divisible by 4!\n";
}
if (num % 5 == 0)
{
cout << "Divisible by 5!\n";
}
if (num % 6 == 0)
{
cout << "Divisible by 6!\n";
}
if (num % 7 == 0)
{
cout << "Divisible by 7!\n";
}
if (num % 8 == 0)
{
cout << "Divisible by 8!\n";
}
if (num % 9 == 0)
{
cout << "Divisible by 9!\n";
}
else
{
cout << "Sorry not divisible by anything!!\n";
}
That else statement applies only to the immediately preceding if statement (check for divisibility by 9). All those if statements should be an if-else if chain.
Maybe I can come up with a statement for example: else if (num % 2,3,4,5,6,7,8,9 !=0) but it did not work either! Can anyone suggest anything?
And I am sorry but I have no clue what you guys all mean when you tell me use code tags!!???
You should not post the same question multiple times. Keeping your question to a single thread avoids confusion. If you don't get any responses in a reasonable time, you can bump your thread.