gpt4 book ai didi

c - 当 x < y 时,余数运算 (x % y) 做什么?

转载 作者:行者123 更新时间:2023-11-30 21:37:48 25 4
gpt4 key购买 nike

该程序正确打印数字是偶数还是奇数......

#include <stdio.h>
int main(void)
{
int n;
printf("Please enter a number:");
scanf("%d", &n);

if(n % 2 == 0)
printf("%d is even", n);
else
printf("%d is odd",n);

return 0;
}

我不明白怎么办n % 2n 时可以给出有意义的结果小于二。 %是余数运算,对吗?如果n小于二,你怎么能把它除以二呢?

最佳答案

I am unable to understand the logic of n%2==0. If user input a value less than 2. Then how it give us correct answer?

运算符%执行模(或余数)运算。一个数除以 2(当该数小于 2 时)的余数就是该数本身(商为 0)。例如,一除二的商为 0,余数为 1,因此 1%2 = 1。

关于c - 当 x < y 时,余数运算 (x % y) 做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com