Writing the code to convert numeric amount of price into English words.
Sigiloso
char *basics[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; char *tens[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; // [0-99] char *basic(char *buf, int n) { if (n <= 19) return basics[n]; strcpy(buf, tens[n/10]); if (n%10) sprintf(buf, "%s %s", buf, basics[n%10]); return buf; } void printPrice(double price) { int N = (int) price; // dollars int M = 100 * (price + 0.005 - (double)N); // cents // extracting the different parts int num_million_thousand = N / (1000000 * 1000); N -= num_million_thousand * (1000000 * 1000); int num_million_hundred = N / (1000000 * 100); N -= num_million_hundred * (1000000 * 100); int num_million_basic = N / 1000000; N -= num_million_basic * 1000000; int num_thousand_hundred = N / (1000 * 100); N -= num_thousand_hundred * (1000 * 100); int num_thousand = N / 1000; N -= num_thousand * 1000; int num_hundred = N / 100; N -= num_hundred * 100; int num_basic = N; // printing the different parts char buff[81] = ""; if (num_million_thousand) printf(" %s thousand", basic(buff, num_million_thousand)); if (num_million_hundred) printf(" %s hundred", basic(buff, num_million_hundred)); if (num_million_basic) printf(" %s", basic(buff, num_million_basic)); if ((int)price / 1000000) printf(" million"); if (num_thousand_hundred) printf(" %s hundred", basic(buff, num_thousand_hundred)); if (num_thousand) printf(" %s thousand", basic(buff, num_thousand)); if (num_hundred) printf(" %s hundred", basic(buff, num_hundred)); if (num_basic && ((int)price / 10) || !((int)price / 10)) printf(" %s", basic(buff, num_basic)); printf(" dollars %s cents", basic(buff, M)); }