Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查/运算符在C?中是否没有余数;_C_Operator Keyword - Fatal编程技术网

如何检查/运算符在C?中是否没有余数;

如何检查/运算符在C?中是否没有余数;,c,operator-keyword,C,Operator Keyword,我想检查运算符是否没有余数: int x = 0; if (x = 16 / 4), if there is no remainder: then x = x - 1; if (x = 16 / 5), if remainder is not zero: then x = x + 1; 如何检查C中是否有余数?和 如何实现它?为此,请使用模块运算符 如果(x%y==0)则没有余数 在除法运算中,如果结果是浮点,则只返回整数部分,而丢弃小数部分。首先,您需要%余数运算符

我想检查
运算符是否没有余数:

int x = 0;    
if (x = 16 / 4), if there is no remainder: 
   then  x = x - 1;
if (x = 16 / 5), if remainder is not zero:
   then  x = x + 1;
如何检查
C
中是否有余数?和

如何实现它?

为此,请使用模块运算符

如果(x%y==0)
则没有余数


在除法运算中,如果结果是浮点,则只返回整数部分,而丢弃小数部分。

首先,您需要
%
余数运算符:

if (x = 16 % 4){
     printf("remainder in X");
}
注意:它不适用于float/double,在这种情况下,您需要使用

第二,按照您的意愿实施:

  • 如果(x=16/4)
    ,如果没有余数,
    x=x-1
  • 如果(x=16/5)
    ,则
    x=x+1
    使用
    逗号运算符,您可以按如下步骤进行操作(阅读注释):

    检查工作代码@codepade:,。
    注意,在if条件下,我使用逗号运算符:
    ,要理解
    运算符读:。

    您可以使用哪个运算符处理余数。

    模运算符(用C中的%符号表示)计算余数。因此:

    x = 16 % 4;
    
    x将是0

    X = 16 % 5;
    

    x将为1

    使用%运算符查找除法的剩余部分

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

    如果要查找整数除法的余数,则可以使用模数(
    %
    ):

    if (number % divisor == 0)
    {
    //code for perfect divisor
    }
    else
    {
    //the number doesn't divide perfectly by divisor
    }
    
    if( 16 % 4 == 0 )
    {
       x = x - 1 ;
    }
    else
    {
       x = x +1 ;
    }