I have solve a basic problem in c that is count the digits in integer and I have written -
#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; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
CodePudding user response:
If the number n
is not divisible by 10
then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0
.
Also the variable i
is not initialized.
int i;
You should write
int i = 0;
do
{
i;
} while ( n /= 10 );
printf( "%d\n", i );