21

Which is the best way to find out whether the division of two numbers will return a remainder? Let us take for example, I have an array with values {3,5,7,8,9,17,19}. Now I need to find the perfect divisor of 51 from the above array. Is there any simpler way to solve this?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Vivek
  • 4,526
  • 17
  • 56
  • 69
  • 2
    I find the range `{3,5,7,8,9,17,19}` quite suspicious. It *almost* looks like you're using primes here, but 8 isn't prime and 11 and 13 are missing... – DarkDust Aug 09 '11 at 10:05

3 Answers3

33

You can use the % operator to find the remainder of a division, and compare the result with 0.

Example:

if (number % divisor == 0)
{
    //code for perfect divisor
}
else
{
    //the number doesn't divide perfectly by divisor
}
Mircea Nistor
  • 3,145
  • 1
  • 26
  • 32
  • 6
    Modulus and reminder are different for negative numbers: http://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder – calandoa Jan 19 '16 at 14:57
  • 1
    good point, but I don't see how that changes the check for perfect division – Mircea Nistor Jan 20 '16 at 14:44
  • 1
    The answer is fine regarding to the question, but the C is very fuzzy about the relation between %, modulo and remainder operators, and your statement was just a bit imprecise, so I just added a link to clarify this point. – calandoa Jan 21 '16 at 13:27
11

Use the modulus operator %, it returns the remainder.

int a = 5;
int b = 3;

if (a % b != 0) {
   printf("The remainder is: %i", a%b);
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • @Joey - why did you remove the example? – MByD Aug 09 '11 at 09:27
  • I didn't; I just corrected a typo and added the operator sign. Could be that simultaneous edits were clashing here. SO is even poorer at that than MediaWiki. I.e. when I was editing it I only saw the one sentence and didn't get a notification either on concurrent modification. Sorry :/ – Joey Aug 09 '11 at 09:34
  • My apologies... I thought you had some problem with my example... And thanks! – MByD Aug 09 '11 at 09:38
  • But it's inverted? for example: 12 % 8 = 4, that's what it should be. but 11 % 8 = 3 when it should equal 5. – MarcusJ Jun 22 '15 at 13:08
  • @MarcusJ - `11 % 8` equals 3, not 5. as `11 = (8 * 1) + 3`. – MByD Jun 22 '15 at 14:08
  • Yeah, I said that it equals 3. for my purpose it should equal 5 (I know how crazy that sounds) because I'm trying to find how many bits until the next byte. if you have read 11 bits, there will be 5 left over until you hit 16, or 2 bytes. it seems like 11 % 8 should work, because 8 is the number of bits in a byte, but apparently I'm getting something wrong. – MarcusJ Jun 22 '15 at 18:58
  • Actually '%' returns the modulo, not the remainder, which is different for negative numbers. – BP8467 Mar 02 '16 at 18:45
1

All the above answers are correct. Just providing with your dataset to find perfect divisor:

#include <stdio.h>

int main() 
{

int arr[7] = {3,5,7,8,9,17,19};
int j = 51;
int i = 0;

for (i=0 ; i < 7; i++) {
    if (j % arr[i] == 0)
        printf("%d is the perfect divisor of %d\n", arr[i], j);
}

return 0;
}
hari
  • 9,439
  • 27
  • 76
  • 110
  • The OP should probably clarify whether 3 or 9 should be considered the perfect divisor in those cases. I assume 9 because otherwise there is no point in it being in the array. In which case you'd need to loop in reverse order. – tinman Aug 09 '11 at 10:20