In objective c (Mac development). How can i find out if a number is divisible by another number and not a decimal?
A number is divisible by another number if it can be divided equally by that number; that is, if it yields a whole number when divided by that number. For example, 6 is divisible by 3 (we say "3 divides 6") because 6/3 = 2, and 2 is a whole number.
C supports a modulo operator % , that evaluates remainder on division of two operands. You can use this to check if a number is exactly divisible by some number or not. For example - if(8 % 2) , if the given expression evaluates 0 , then 8 is exactly divisible by 2.
The divisibility rule of 7 states that, if a number is divisible by 7, then “the difference between twice the unit digit of the given number and the remaining part of the given number should be a multiple of 7 or it should be equal to 0”. For example, 798 is divisible by 7. Explanation: The unit digit of 798 is 8.
A number is divisible by 3 if sum of its digits is divisible by 3. Illustration: For example n = 1332 Sum of digits = 1 + 3 + 3 + 2 = 9 Since sum is divisible by 3, answer is Yes.
Assuming you're dealing with integers, use the modulus function:
if ((a % b) == 0) {
// A is divisible by B
} else {
// A isn't divisible by B
}
I'm not a objective c programmer, but in general, just checking if A mod B = 0 should do it... won't it work in OC?
cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With