Duy Nguyen Duy Nguyen - 1 month ago 22
C Question

Cannot print '1'?

I'm new to programming and I was doing an exercise in school. For my following program where I am stuck:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main() {
int n;
printf("Input the integer number:");
scanf("%d",&n);
int usevar=n;
int count=0;
while(n>1)
{
n/=10;
count++;
}
int sum=0;
printf("Reverse:\n");
for(int i=1;i<=count;i++)
{
int a=pow(10,i);
int b=pow(10,i-1);
sum=(usevar%a-usevar%b)/b;
printf("i=%d\n",sum);
}
return 0;
}


When I inserted some int number such as
12345
the output should be:

Reverse:

i=5

i=4

i=3

i=2

i=1


But the output of my program is:

Reverse:

i=5

i=4

i=3

i=2


But when I input the number without '1' as start number, my program works correctly:

Input the integer number:23556

Reverse:

i=6

i=5

i=5

i=3

i=2


So what's the problem with the number that start with '1'? Can anyone give me the suggestion? I'm glad if you can help.

Answer

try this,

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main() {

int n;
printf("Input the integer number:");
scanf("%d",&n);

while(n > 0){
    int rev = n%10;
    n = n/10;
    printf("i = %d\n",rev);
}

}