Number to Word Converter is a small program to show numbers in their word form. In many systems, numerical data needs to be represented into the words. We can take example of cheques the amount is written in both digits(numerical) and words. This program is created in purely C language, Basically it takes input in number and then show its word form.
Some Examples are as follows :
- 799 = Seven Hundred Ninety Nine.
- 1009 = One Thousand Nine.
- 0018 = Eighteen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
/* Author - Nisar Ahmed Description: Converts a number to word form. Input - Number in digits Output - Number in words */ #include< stdio.h> #include< conio.h> #include< stdlib.h> #include< string.h> char word[100]=""; char *oneDigit[10] = {"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "}; char *teenNumbers[10] = {"Ten ","Eleven ","Tweleve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "}; char *twoDigit[10] = {"","","Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "}; int check(char *c); char * cutter(char *c,int start); char * subString(char *str,int start,int end); char *Toword799(char *c); void shownum(char *p); void main(){ char *num,ch; int i; textbackground(RED); textcolor(WHITE); clrscr(); do{ clrscr(); window(1,1,80,25); gotoxy(20,8); printf("#########################################"); gotoxy(20,9); printf("#### 799 Web Nixar's Creation ####"); gotoxy(20,10); printf("####---------------------------------####"); gotoxy(20,11); printf("#### Number to Word Converter ####"); gotoxy(20,12); printf("#########################################"); gotoxy(20,13); printf("## ##"); gotoxy(20,14); printf("## 1 => Enter Number ##"); gotoxy(20,15); printf("## 2 => Exit ##"); gotoxy(20,16); printf("## ##"); gotoxy(20,17); printf("## Enter Choice ##"); gotoxy(43,17); scanf("%c",&ch); switch(ch){ case '1': gotoxy(20,18); printf("## Enter Number ##"); gotoxy(43,18); scanf("%s",num); if(check(num)==0){ shownum(Toword799(num)); strcpy(word,""); }else{ gotoxy(27,19); printf("Please ensure that you entered\n"); gotoxy(27,20); printf("a number between 0-999999999!!!"); getch(); } break; case '2':exit(0); break; default:main(); } }while(1); getch(); } // Functio for the validation int check(char *c){ int flag=0,i; if(strlen(c) > 9){ return 1; } for(i=0;i< strlen(c);i++){ if(!(c[i]> = '0' && c[i] < ='9')) flag=1; } return flag; } // Return sub string from given start Index to the end of string. char * cutter(char *c,int start){ char *p; int i; for(i=0;i< strlen(c)-start;i++) p[i]=c[i+start]; p[i]='\0'; return p; } // Return sub string from given start index to end Index. char * subString(char *str, int start,int end){ char *substr; int i,j; for(j=0,i=start;i < end;i++,j++) substr[j] = str[i]; substr[j] = '\0'; return substr; } // Recursive Method to create word conversion of the given number. char* Toword799(char *num){ if(strlen(num)==9){ if(num[0]=='1'){ char *numberInString = subString(num,1,2); int indexOfDigit = atoi(numberInString); strcat(word,teenNumbers[indexOfDigit]); }else{ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,twoDigit[indexOfDigit]); numberInString = subString(num,1,2); indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); } if(num[0]=='0' && num[1]=='0'){ }else{ strcat(word,"Crore "); } num = cutter(num,2); Toword799(num); }else if(strlen(num)==8){ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); if(num[0]=='0'){ }else{ strcat(word,"Crore "); } num = cutter(num,1); Toword799(num); }else if(strlen(num)==7){ if(num[0]=='1'){ char *numberInString = subString(num,1,2); int indexOfDigit = atoi(numberInString); strcat(word,teenNumbers[indexOfDigit]); }else{ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,twoDigit[indexOfDigit]); numberInString = subString(num,1,2); indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); } if(num[0]=='0' && num[1]=='0'){ }else{ strcat(word,"Lacs "); } num = cutter(num,2); Toword799(num); }else if(strlen(num)==6){ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); if(num[0]=='0'){ }else{ strcat(word,"Lacs "); } num = cutter(num,1); Toword799(num); }else if(strlen(num)==5){ if(num[0]=='1'){ char *numberInString = subString(num,1,2); int indexOfDigit = atoi(numberInString); strcat(word,teenNumbers[indexOfDigit]); }else{ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,twoDigit[indexOfDigit]); numberInString = subString(num,1,2); indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); } if(num[0]=='0' && num[1]=='0'){ }else{ strcat(word,"Thousand "); } num = cutter(num,2); Toword799(num); }else if(strlen(num)==4){ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); if(num[0]=='0'){ }else{ strcat(word,"Thousand "); } num = cutter(num,1); Toword799(num); }else if(strlen(num)==3){ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); if(num[0]=='0'){ }else{ strcat(word,"Hundred "); } num = cutter(num,1); Toword799(num); }else if(strlen(num)==2){ if(num[0]=='1'){ char *numberInString = subString(num,1,2); int indexOfDigit = atoi(numberInString); strcat(word,teenNumbers[indexOfDigit]); }else{ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,twoDigit[indexOfDigit]); numberInString = subString(num,1,2); indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); } num = cutter(num,2); Toword799(num); }else if(strlen(num)==1){ char *numberInString = subString(num,0,1); int indexOfDigit = atoi(numberInString); strcat(word,oneDigit[indexOfDigit]); num = cutter(num,1); Toword799(num); }else{ if(strcmp(word,"")==0){ strcat(word,"Zero"); }else{ return word; } } return word; } // Function to show the converted word of the number void shownum(char *p){ textbackground(BLUE); textcolor(WHITE); clrscr(); window(10,13,70,16); cprintf("%s",p); getch(); } |