How to find the remainder of a division in C?

Vivek picture Vivek · Aug 9, 2011 · Viewed 161.9k times · Source

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?

Answer

Mircea Nistor picture Mircea Nistor · Aug 9, 2011

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
}