C++ program to check if the range between two numbers is divisible by another number

delete this
Last edited on
So let's make sure I got this right. If a = 5 , b = 10 , and c = 2, you want to see if any number between 5 and 10 will be divisible by 2?

If that's the case, the code you have now is fine... Unless you input "a" as a larger number than "b". What's the issue?
I want it to output 1 Yes or 1 No
cout << "1 NO" << endl; //like this?!

a side note, you can drastically reduce the work being done: you don't even need a loop.
the remainder vs the result...
say you want to know if something in 1234 to 1300 is divisible by 9.
1234 % 9 is 1
so we know that if we just added 1 to 1234, it would be divisible.
1235 / 9 = 137
and we know that 1235 is in the range asked for.
we are done. Do you understand this? Modulo is not well taught ... do you get what the remainder represents and why this is true? If you understand that... does anything change if your numbers are sometimes negative?
whole thing becomes if (a+a%c <= b) I believe, for positive input where a < b
Last edited on
jonnin Sorry thats not useful
Just funny to see, in a C++ forum

delete this

:)
Well I can't delete it myself so if any mod or anything can delete that would be cool..
typically you leave your question and its answers for others to enjoy. Deleting posts is usually seen as rude, unless the post is spam or unacceptable in some way.

if you do it too much people stop trying to help.

I think what he wanted was break; If he found a number that was divisible, than it should stop the loop.
> say you want to know if something in 1234 to 1300 is divisible by 9.
> 1234 % 9 is 1
> so we know that if we just added 1 to 1234, it would be divisible.
> 1235 / 9 = 137
you've got it backwards
you need to subtract 1 to 1234 so it would be divisible
1233 / 9 = 137

then you may do 1233 + 9 to get a number in the desired range.
Last edited on
Deleting posts is usually seen as rude, unless the post is spam or unacceptable in some way.

Someone might actually consider reporting the poster in question.
well that would explain why it wasn't helpful. I hate when I do that.
Someone might actually consider reporting the poster in question.

Which, in this case, would be giving the poster exactly what they wanted... in effect, rewarding them for abusing this forum.

Sounds kinky
Topic archived. No new replies allowed.