#include #include #define MAX 100000 #define CRORE 7 #define LAKH 5 #define THOUSAND 3 #define HUNDRED 2 #define TEN 1 /* returns inside str[] the word corresponding to single digit 1-9 */ void unitString ( int n, char str[ ] ) { switch(n) { case 0: strcpy(str, ""); break; case 1: strcpy(str, " one"); break; case 2: strcpy(str, " two"); break; case 3: strcpy(str, " three"); break; case 4: strcpy(str, " four"); break; case 5: strcpy(str, " five"); break; case 6: strcpy(str, " six"); break; case 7: strcpy(str, " seven"); break; case 8: strcpy(str, " eight"); break; case 9: strcpy(str, " nine"); break; } } /* returns inside str[] the word corresponding to teen digits 11-19 */ void teenString ( int n, char str[ ] ) { switch(n) { case 0: strcpy(str, ""); break; case 1: strcpy(str, " eleven"); break; case 2: strcpy(str, " twelve"); break; case 3: strcpy(str, " thirteen"); break; case 4: strcpy(str, " fourteen"); break; case 5: strcpy(str, " fifteen"); break; case 6: strcpy(str, " sixteen"); break; case 7: strcpy(str, " seventeen"); break; case 8: strcpy(str, " eighteen"); break; case 9: strcpy(str, " nineteen"); break; } } /* returns inside str[] the word corresponding to tens digits 10,20,30,40,50,60,70,80,90 */ void tensString ( int n, char str[ ] ) { switch(n) { case 0: strcpy(str, ""); break; case 1: strcpy(str, " ten"); break; case 2: strcpy(str, " twenty"); break; case 3: strcpy(str, " thirty"); break; case 4: strcpy(str, " forty"); break; case 5: strcpy(str, " fifty"); break; case 6: strcpy(str, " sixty"); break; case 7: strcpy(str, " seventy"); break; case 8: strcpy(str, " eighty"); break; case 9: strcpy(str, " ninety"); break; } } /* handles word generation for two consecutive digits */ void genTwoDigitString ( char num[ ], int l, char str[ ], int b ) { char s[MAX]; int teenFlag = 0; s[0] = '\0'; if ( l > b ) { if ( (num[l-b-1]-'0' == 1) && (num[l-b]-'0' > 0) ) teenFlag = 1; else { tensString(num[l-b-1]-'0', s); strcat(str, s); } } s[0] = '\0'; if ( l > b-1 ) { if ( teenFlag ) { teenString(num[l-b]-'0', s); teenFlag = 0; } else unitString(num[l-b]-'0', s); strcat(str, s); } } /* handles word generation within every crore boundaries */ void genBoundedWordString ( char num[ ], int l, char str[ ] ) { char s[MAX]; /* handle numbers inside 100000-9999999 */ s[0] = '\0'; genTwoDigitString (num, l, s, LAKH+1); if ( l > LAKH && s[0] != '\0' ) { strcat(str, s); strcat(str, " lakh"); } /* handle numbers inside 1000-9999 */ s[0] = '\0'; genTwoDigitString (num, l, s, THOUSAND+1); if ( l > THOUSAND && s[0] != '\0' ) { strcat(str, s); strcat(str, " thousand"); } /* handle numbers inside 100-999 */ s[0] = '\0'; if ( l > HUNDRED ) { unitString(num[l-HUNDRED-1]-'0', s); if ( s[0] != '\0' ) { strcat(str, s); strcat(str, " hundred"); } } /* handle numbers inside 1-99 */ s[0] = '\0'; genTwoDigitString (num, l, str, TEN); } /* generates the word string corresponding to the full number */ void numToWordString ( char num[ ], int l, char str[ ] ) { if ( l > CRORE ) { numToWordString(num, l-CRORE, str); // recursive call across crore boundaries strcat(str, " crore"); } genBoundedWordString(num, l, str); // handle number inside each crore } /* generates the digit string corresponding to each digit in the full number */ void numToDigitString ( char num[ ], int l, char str[ ] ) { char s[MAX]; if ( l > 0 ) { numToDigitString(num, l-1, str); s[0] = '\0'; unitString(num[l-1]-'0', s); if ( s[0] != '\0' ) strcat(str, s); else strcat(str, " zero"); } } int main () { char number[MAX], string[MAX]; printf("Enter Number: "); scanf("%s", number); string[0] = '\0'; numToDigitString(number, strlen(number), string); printf("\nDigit Sequence: %s\n", string); string[0] = '\0'; numToWordString(number, strlen(number), string); printf("\nNumber in Word: %s\n", string); return 0; }