[英]why n mod 10 in loop doesn't show output
I have solve a basic problem in c that is count the digits in integer and I have written -我已经解决了 c 中的一个基本问题,即计算整数中的数字,我写了 -
#include<stdio.h> #include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10;我已经知道上面的代码是错误的,我应该写n/=10; instead of n%=10;
而不是 n%=10; but I wants to know why it is not printing even value of i ie 0.
但我想知道为什么它不打印 i 的偶数值,即 0。
If I have written any wrong so please ignore it ,I am new here..如果我写错了请忽略它,我是新来的..
If the number n
is not divisible by 10
then the value of this expression (the remainder of the division)如果数字
n
不能被10
整除,则此表达式的值(除法的余数)
n %= 10;
will never be equal to 0
.永远不会等于
0
。
Also the variable i
is not initialized.变量
i
也没有初始化。
int i;
You should write你应该写
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.