Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: How do i find out if a number is divisible by another number?

In objective c (Mac development). How can i find out if a number is divisible by another number and not a decimal?

like image 524
Daniel Avatar asked Aug 12 '10 12:08

Daniel


People also ask

How do you determine if a number is divisible by another number?

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.

How do you find if a number is divisible by another number in C?

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.

How do you know if something is divisible by 7?

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.

How do you check if a number is only divisible by 3?

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.


2 Answers

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
}
like image 107
David Knell Avatar answered Sep 23 '22 20:09

David Knell


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.

like image 28
filippo Avatar answered Sep 19 '22 20:09

filippo