Public Group

Basic Math Problem

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

How can I tell if a certain number is a divisible of another number? for Example, How can I tell if 32 is a divisible of 4 and 33 isn´t? I´m using C++

Share on other sites
bool divisible(int dividend, int divisor) {   if (dividend%divisor==0) {      return true;   } else {      return false;   }}

The modulus % operator returns the remainder of a division problem.

Share on other sites
Use the mod operator to check if the remainder is 0.

Example
32 % 4 = 0 // divisable
33 % 4 = 1 // not divisable

Share on other sites
Quote:
 Original post by Lawl_Rockbool divisible(int dividend, int divisor) { if (dividend%divisor==0) { return true; } else { return false; }}The modulus % operator returns the remainder of a division problem.

Or just:
bool isDivisible(int dividend, int divisor){   return dividend%divisor == 0;}

:)

Share on other sites
Oh that's nice :)
I love shorter ways to implement things.

Share on other sites
Quote:
 Original post by Lawl_RockI love shorter ways to implement things.

Then prepare to have a crush on this:
bool isDivisible(int dividend, int divisor){   return !(dividend%divisor);}

Share on other sites
But is 5 divisible by 0? Since 5/0 is undefined, I would say it is not.
bool isDivisible(int divident, int divisor){  return (divisor != 0) && (divident % divisor == 0);}

or shorter and a bit more cryptic
bool isDivisible(int x, int y){  return y && !(x % y);}

Share on other sites
Also,

if I devide an an int and I get a number with a decimal point what happens, does the compiler round up to the nearest whole number or does it just ignore the decimal point?

Share on other sites
Quote:
 Original post by JazonxyzAlso,if I devide an an int and I get a number with a decimal point what happens, does the compiler round up to the nearest whole number or does it just ignore the decimal point?

With integer division, the part after the decimal point is ignored. For example, 8 / 5 == 1. No rounding is done.

Share on other sites
#include &lt;iostream&gt;#include &lt;iomanip&gt;void test(int x, int y){    std::cout &lt;&lt; std::setw(4) &lt;&lt; x &lt;&lt; " / " &lt;&lt; std::setw(4) &lt;&lt; y &lt;&lt; " = " &lt;&lt; std::setw(4) &lt;&lt; (x / y) &lt;&lt; " --- " &lt;&lt; std::setw(5) &lt;&lt; (float(x) / float(y)) &lt;&lt; std::endl;}int main(){    test(9, 4);    test(-9, 4);    test(9, -4);    test(-9, -4);}

prints
   9 /    4 =    2 ---  2.25  -9 /    4 =   -2 --- -2.25   9 /   -4 =   -2 --- -2.25  -9 /   -4 =    2 ---  2.25

Meaning it drops the part after the decimal point when dividing integers.

1. 1
2. 2
Rutin
29
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• Forum Statistics

• Total Topics
633647
• Total Posts
3013108
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!