I'm creating a code to transform integers into roman numerals. I thought about attempting to use getchar() to read each number but then decided to try to use scanf and use algebraic means to break the number into components for a string. Below is the code, which seems to work fine except for this error. I commented in the code several values that return wrong.

Code:#include <stdio.h> #include <math.h> #define MAXNUM 4 int main() { double input, fraction, integer; int i, j; int b10_num[MAXNUM]; char *N_1000[] = {"", "M", "MM"}; char *N_100[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; char *N_10[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; char *N_1[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; while(1) { printf("\nEnter a number (between 1 and 3999): "); scanf("%lf", &input); if (input < 4000 && input > 0 && modf(input, &integer) == 0) break; else { printf("Error: number is invalid"); continue; } } //transforming into string /* ERROR LIST 1551 returns 1550 1552 returns 1551 1556 returns 1555 1557 returns 1556 1560 returns 1550 */ for(i = 0; i < MAXNUM; i++) { input = input / 10; printf("input: %g\t", input); fraction = modf(input, &integer) * 10; b10_num[i] = (int)fraction; printf("fraction: %lf\t b10_num[%d]: %d\n", fraction, i, b10_num[i]); } input *= pow(10,MAXNUM); //result printf("Digits of the number: "); for(j = 3; j >= 0; j--) { printf("%d,", b10_num[j]); } printf("\nThe number %g as Roman numeral: %s%s%s%s\n", input, N_1000[b10_num[3]], N_100[b10_num[2]], N_10[b10_num[1]], N_1[b10_num[0]]); return 0; }