Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the remainder of a division in C?

Tags:

c

division

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?

like image 480
Vivek Avatar asked Aug 09 '11 09:08

Vivek


1 Answers

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
}
like image 100
Mircea Nistor Avatar answered Sep 28 '22 06:09

Mircea Nistor