如何找到C中除法运算的余数? [英] How to find the remainder of a division in C?

查看:85
本文介绍了如何找到C中除法运算的余数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方法是找出两个数字相除是否会返回余数的最佳方法?
让我们举个例子,我有一个值{3,5,7,8,9,17,19}的数组。现在,我需要从上面的数组中找到51的完美除数。有没有更简单的方法来解决此问题?

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?

推荐答案

您可以使用运算符找到除法的余数,并将结果与​​ 0 进行比较。

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

示例:

if (number % divisor == 0)
{
    //code for perfect divisor
}
else
{
    //the number doesn't divide perfectly by divisor
}

这篇关于如何找到C中除法运算的余数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆