I’d like the following code to work:
double num = 31415; /* num will never have a decimal part */
/* ... code here so that we can now say */
printf("%d", num_array[0] == 3 && num_array[1] == 1 && num_array[4] == 5); //1
I realize it’s trivial to do this with ints (just int i=0; while(num>0) numarr[size-++i] = num%10, num/=10; where size is predetermined to be the number of digits in the number), but that obviously won’t work for floats/doubles because you can’t mod one floats.
And yes, I need to use floats/doubles, despite not using the floating point section (it’s an exercise).
And I have figured out how to determine the number of digits in a double, using floor().
/* n > 0, n=floor(n) */
int numdigits(double n){
int i = 0;
while (n >0)
n = floor(n/10), i++;
return i;
}
Look up
fmod. The number of digits will probably be easier to compute usinglog10.