I really need help with this program not working correctly

closed account (4wRjE3v7)
Hi so I made a program using if statements, but when I put 64 as the input it does include all of the correct input except for the last part "Not divisible!" It shows up every time! I really don't know how to fix the problem anyone please care to look over my code and help me. I am new to c++. Please explain nicely, it would help me so much.
THANK YOU!!

#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";

}

else 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";
}
}
Last edited on
You should edit your post and use code tags under the format option.

You're only using one else if.
If you add else if in place of each if statement (except the first one) I think you'll get the results your looking for.

Last edited on
Topic archived. No new replies allowed.