I am trying to write a program that gives output if the input is an integer (say 1234)
1234
234
34
4
I wrote the following code in C using codeblocks IDE :
#include <stdio.h>
#include <math.h>
int main()
{
int a,l=0,b;
print("Enter a number : ");
scanf("%d",&a);
b =a;
while (b>0)
{
l++;
b = b/10;
}
l=l-1;
while(a>0)
{
printf("%d\n",a);
b = (int)pow(10,l);
printf("10^%d = %d\n",l,b ); //Testing statement
a = a % b;
l--;
}
return 0;
}
Initially, I was puzzled seeing the output but to test I wrote the test statement to see what is happening.
I don't think there is some problem with the logic. Because when I put a=1234 and run this I get the following output :
Initially, I thought the problem is due to the pow function which is a double type, so I cast it into int type. Can someone explain what is the error here? I can write my own pow function which returns an int but I want to know why this pow function behaving like this?