I am trying to break down a number and obtain each individual digit. I am trying to do that by dividing by powers of ten until reaching 0. Every time I divide by ten it adds to the total of digits in the integer. Once I have the total number of digits I will divide the original digit by 10 to the number of digits - 1 and then decrease the number of digits and repeat the process. This works all the way up until the last digit where I get 4 instead of 6. I have tried using a different number and the last and sometimes second to last digits are the wrong number. Is there any reason as to why this is doing this and any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int indx;
int originalDigit = 1234;
int numDigits;
int digit;
int individualDigit;
numDigits = 0;
digit = originalDigit;
while(digit > 0)
{
digit = digit / 10;
numDigits++;
}
printf("%d\n", numDigits);
digit = originalDigit;
while( digit > 0)
{
individualDigit = digit / (int)pow(10, numDigits - 1);
printf("%d ", individualDigit);
digit = digit % (int)pow(10, numDigits - 1);
numDigits--;
}
return 0;
}