Reputation:
I'm doing an exercise where it is asking to eliminate any number divisible by 5 that ends up as a whole number. So instead of :
If (num == 5 || num == 10)
continue;
Is there some way to tell the computer if it is divisible by 5 don't output it or if it is odd or even don't output the number? Please try to leave answers at a novice level I am just starting to program.
Upvotes: 0
Views: 155
Reputation: 7663
Yes. A number is divisible by 5 with if the modulus, namely, operator%
returns 0.
You can do:
if (n % 5 == 0) {
//number is divisible by 5
}
else {
//not divisible by 5
}
Upvotes: 2
Reputation: 5685
The modulus operator % gives you the remainder of a division, so you can just check if that is zero
num % 5 == 0
Upvotes: 2
Reputation: 29431
if (num % 5 == 0)
continue;
Will only continue on 5, 10, 15...
Upvotes: 1
Reputation: 227390
bool isDivisibleBy5 = n % 5 == 0;
where n
is your number. For odd numbers,
bool isOdd = n % 2; // evaluates to 0 if even, so false
Upvotes: 3