The problem is given a number (upto a certain range) we need to convert the number in words.
Example:
- 999 -> Nine Hundred Ninety-Nine
- 4 -> Four
- 0 -> Zero
- -230 -> Minus Two Hundred Thirty.
- 40 -> Forty
C Implementation
#include <stdio.h> void print(int arr[], int n) { int m, i; char *unit[] = {"","One","Two","Three","Four","Five","Six", "Seven","Eight","Nine"}; char *ten[] = {"","Ten","Twenty","Thirty","Forty", "Fifty","Sixty","Seventy","Eighty","Ninety"}; char *hundred[] = {"Ten","Eleven","Twelve","Thirteen","Fourteen", "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; for(i = 0; i < 5; i++) { if(i != 3) { if(arr[i]) { if((arr[i]%10) != 0) { if(arr[i] < 10) printf("%s ",unit[arr[i]]); else if(arr[i] <= 19) printf("%s ",hundred[arr[i]%10]); else printf("%s-%s ",ten[arr[i]/10],unit[arr[i]%10]); } else printf("%s ",ten[arr[i]/10]); switch(i) { case 0: printf("Coror ");break; case 1: printf("Lakh ");break; case 2: printf("Thousand "); } } } else if(arr[3]) { printf("%s ",unit[(arr[i])]); printf("Hundred "); } } printf("\b."); } void divide(long int temp) { int arr[10], i; long int div = 10000000; for(i = 0; i < 5; i++) { arr[i] = temp/div; temp = temp%div; if(i == 2) div /= 10; else div /= 100; } print(arr, 10); } int main(void) { int i,j; long int num,temp; printf("Enter A Number(9-dig): "); scanf("%lld",&num); temp = (num < 0)? (-1 * num) : num; printf("In Words: "); if(num < 0) { printf("Minus "); divide(temp); } else if(num == 0) { printf("Zero."); } else { divide(temp); } return 0; }
Enter A Number(9-dig): 3 In Words: Three. Enter A Number(9-dig): 120 In Words: One Hundred Twenty. Enter A Number(9-dig): 0 In Words: Zero. Enter A Number(9-dig): -120 In Words: Minus One Hundred Twenty. Enter A Number(9-dig): 99999 In Words: Ninety-Nine Thousand Nine Hundred Ninety-Nine. Enter A Number(9-dig): 999999 In Words: Nine Lakh Ninety-Nine Thousand Nine Hundred Ninety-Nine. Enter A Number(9-dig): 9999999 In Words: Ninety-Nine Lakh Ninety-Nine Thousand Nine Hundred Ninety-Nine. Enter A Number(9-dig): 002 In Words: Two.