446,189 Members | 904 Online Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

# how to print the numbers in words?

 P: 1 Hi today (9th july 2006) i attended a technical interview in aspire systems. Two rounds of technical interviews. i well in the first round and did bad in the other. The interviewer asked me to write a program to print and given number in words... say for example... if the input is 12345 then the output should be printed as "twelve thousand three hundred and fourty five" can someone help me how to write a program for this in c. waiting for the reply. thanking you. Jul 9 '06 #1
10 Replies

 P: 11 hi there well this problem is quite cool. Well u can split the number this way.(I mean first determine number at the last at once place and proceed down last thousand or so) here is the code Expand|Select|Wrap|Line Numbers int num, temp,lastnumber;   num=12345; temp=num; for(int i=1;temp==0;i++) temp=temp/10;     //to check how many digits are there                            // even if  zero is included in it like 10004                            // so number of digits is equal to i; temp=num; for(int j=1;i==j;j++) { num=num/10;   // since num is integer it will exculde the decimal num=num*10;   // exclude number will be replaced by zero lastnumber[j]=temp-num; temp=num; }  well after code u will have no of digits in the number in ' i ' and the invidudual number in array lastnumber; now is the tricky part of giving words. well observe frist tewenty no " one two three ...... eleven , twelve .... twenty " u have to make string array to store them (array set 1) then array to store these {twenty,thirty,forty.... ninety}(array set 2) then check values a places of tens for examples. if it is 2 or above just conjugate arrayset1&2 to get desired value ;) fot example 54 "fifty" from array set 2 and "four" from array set 1 to make "fifty four" then maybe add "thousand,lakh ....or nothing etc" for number less than twenty use array set 1 directly hope that helped u note:-U may have to modify the script the decipher values having decimal :D Jul 10 '06 #2

 P: 35 Hi today (9th july 2006) i attended a technical interview in aspire systems. Two rounds of technical interviews. i well in the first round and did bad in the other. The interviewer asked me to write a program to print and given number in words... say for example... if the input is 12345 then the output should be printed as "twelve thousand three hundred and fourty five" can someone help me how to write a program for this in c. waiting for the reply. thanking you. Hi, I think the interviewer would have asked you to give the logic for this question becoz writing the program inside the interview cabin for this is very difficult, anyway I am giving the program, you just copy it and paste and run, it will work. Expand|Select|Wrap|Line Numbers #include  #include  #include    void convert(unsigned long Value);   void main(void) {     unsigned long x =1, Val=0, i=1;       if( (x>=100)&&(x<1000) )         i=2;     else if( (x>=1000)&&(x<=99999) )         i=3;     else if( (x>=100000)&&(x<=9999999) )         i=4;     else if( (x>=10000000)&&(x<=999999999) )         i=5;       while(i)     {         if(i==1)         {             convert(x%100);         }         else if(i==2)         {             convert((x%1000)/100);             printf("Hundred ");         }         else if(i==3)         {             convert((x%100000)/1000);             printf("Thousand ");         }         else if(i==4)         {             convert((x%10000000)/100000);             printf("Lakh ");         }         else if(i==5)         {             convert((x%1000000000)/10000000);             printf("Crore ");         }         --i;     }     printf("\n\n"); }   void convert(unsigned long Value) {     unsigned long Val=0, i=0;     char Ones = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};     char Teens = {"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};     char Tens = {"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};       for(i=0;i<2;i++)     {         if(i==0)         {             Val = Value%100;             if(Val<20) i=2;         }         else if(i==1)    Val = Value%10;         if( (Val>=10)&&(Val<=19) )             printf("%s ",&Teens[Val-10]);         else if( (Val>=1)&&(Val<=9) )             printf("%s ",&Ones[Val-1]);         else if( (Val>=20)&&(Val<=99) )             printf("%s ",&Tens[((Val-(Val%10))/10)-2]);     } } Jul 12 '06 #3

 P: 2 Expand|Select|Wrap|Line Numbers void convert(unsigned long Value) { unsigned long Val=0, i=0; char Ones = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"}; char Teens = {"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; char Tens = {"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};   for(i=0;i<2;i++) { if(i==0) { Val = Value%100; if(Val<20) i=2; } else if(i==1) Val = Value%10; if( (Val>=10)&&(Val<=19) ) printf("%s ",&Teens[Val-10]); else if( (Val>=1)&&(Val<=9) ) printf("%s ",&Ones[Val-1]); else if( (Val>=20)&&(Val<=99) ) printf("%s ",&Tens[((Val-(Val%10))/10)-2]); } } ------------------------------------------------------------------------------------------------------------------- upstair, what does this code mean? can translate to me ??? Sep 1 '06 #4

 P: 2 ------------------------------------------------------------------------------------------------------------------ int num, temp,lastnumber; num=12345; temp=num; for(int i=1;temp==0;i++) temp=temp/10; //to check how many digits are there // even if zero is included in it like 10004 // so number of digits is equal to i; temp=num; for(int j=1;i==j;j++) { num=num/10; // since num is integer it will exculde the decimal num=num*10; // exclude number will be replaced by zero lastnumber[j]=temp-num; temp=num; } well after code u will have no of digits in the number in ' i ' and the invidudual number in array lastnumber; ------------------------------------------------------------------------------------------------------------- could you please explain these more...? Mar 8 '07 #5

 10K+ P: 13,264 ----------------------------------------------------------------------------- could you please explain these more...? Which part do you not understand? Mar 8 '07 #6

 P: 94 Well could you please read the instructions and put your code in tags please, you're giving me a headache and I would really like to read the code... For example Expand|Select|Wrap|Line Numbers   Code goes here, isn't this easier to read?  ;-) Mar 8 '07 #7

 P: 2 Which part do you not understand? On how to split the number this way.(I mean first determine number at the last at once place and proceed down last thousand or so) Mar 14 '07 #8

 P: n/a Expand|Select|Wrap|Line Numbers /*************************************************************  program to convert given integer to words  Date:23/10/2010,Day:Saturday,Rakesh *************************************************************/ #include char str={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN",                                     "EIGHT","NINE"}; char s={"ELEVEN","TWELEVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",                "SEVENTEEN","EIGHTEEN","NINTEEN"}; char ss={"TEN","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY",             "NINTY"}; int main() {    long int num;    int n,r,c=0;    printf("Enter the number:\n");    scanf("%ld",&num);    n=num;    while(n) {     r=n%10;     n=n/10;          c++;    }    switch(c)   {     case 1:unit(num);break;     case 2:tens(num);break;     case 3:hundreds(num);break;     case 4:thousand(num);break;     case 5:ten_thousand(num);break;     case 6:lak(num);break;     case 7:ten_lak(num);break;     case 8:crore(num);break;     case 9:ten_crore(num);break;     }     printf("\n");   return 0; } int unit(int num)  {     num?printf("%s ",str[num-1]):printf("ZERO "); } int tens(int num)  {         (num>10&&num<20)?printf("%s ",s[num-11]):(printf("%s ",ss[num/10-1])     &&((num%10)?unit(num%10):1)); } int hundreds(int num)  { (num/100)?(unit(num/100)&&printf("HUNDRED AND ")):1;      (num%100)?tens(num%100):1;     } int thousand(int num)  {     (num/1000)?(unit(num/1000)&&printf("THOUSAND ")):1;     (num%1000)?hundreds(num%1000):1; } int ten_thousand(int num) {     (num/1000)?(tens(num/1000)&&printf("THOUSAND ")):1;     (num%1000)?hundreds(num%1000):1; } int lak(int num) {     (num/100000)?(tens(num/10000)&&printf("LAKHS ")):1;     (num%100000)?ten_thousand(num%100000):1;     } int ten_lak(int num)  {     (num/100000)?(tens(num/100000)&&printf("LAKHS ")):1;     (num%100000)?ten_thousand(num%100000):1; } int crore(int num)  {     (num/10000000)?(tens(num/10000000)&&printf("CRORE ")):1;        (num%1000000)?ten_lak(num%10000000):1; } int ten_crore(int num)  {     (num/10000000)?(tens(num/10000000)&&printf("CRORE ")):1;     (num%10000000)?ten_lak(num%10000000):1;             } Oct 28 '10 #9

 P: 1 Here's my Code its C..programming.. Expand|Select|Wrap|Line Numbers #include   #include  #include  #include  char *make_words(char *s, int ncomma), *insert_comma(long n, int *ncomma),*int2words(int n); int a; main(void) {printf("\nEnter number to be converted to words: ");  scanf("%d",&a);  printf("\n\nEquivalent in words is:\n\n   \t\t\t%s ! \n",int2words(a)); getch();   return 0;} char *make_words(char *s, int ncomma) {   int i, len, rest = 0;   char *p = NULL;   static char zzz;   static char *ones[] = {"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};   static char *tens[] = {"Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};   static char *twenties[] = {"","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};   static char *hundreds[] = {"Hundred ","Thousand ","Million "}; memset(zzz, '\0', 256); len = strlen(s); for(i = 0; i < len; i++) {if ((p = strchr((s[i] == ',') ? &s[++i] : &s[i], ',')) == NULL){ p = &s[strlen(s)];} if (s[i] == '0'){ continue;} if ((rest = (p - &s[i])) != 0){ if (rest == 3){ strcat(zzz, ones[s[i] - '0' - 1]); strcat(zzz, hundreds); if (len == 7 && s == '0')  strcat(zzz, hundreds); if (len == 11 && s == '0')  strcat(zzz, hundreds);} else if (rest == 2) { if (s[i] == '1'){ strcat(zzz, tens[s[++i] - '0']); rest--;} else{ strcat(zzz, twenties[s[i] - '0' - 1]);}} else strcat(zzz, ones[s[i] - '0' - 1]);} if (rest == 1 && ncomma != 0){ strcat(zzz, hundreds[ncomma--]);}} return zzz;} char *insert_comma(long n, int *ncomma){ static char zzz; int i = 0; char *p = &zzz[sizeof(zzz)-1]; *p = '\0'; *ncomma = 0; do { if (i % 3 == 0 && i != 0) { *--p = ','; ++*ncomma;} *--p = (char)('0' + n % 10); n /= 10; i++;} while(n != 0); return p;} char *int2words(int n){ int nc; char *ps, *zzz, *minus; char *buffer, *v; buffer = (char *) malloc(256); if (n <= 0){ minus = " Zero"; n = abs(n);} else{ minus = "";} ps = insert_comma(n, &nc); zzz = make_words(ps, nc); sprintf(buffer,"%s %s", minus, zzz); return buffer; getch(); return 0;}   Good Luck! Jan 30 '11 #10

 P: 1 Expand|Select|Wrap|Line Numbers #include  #include        void printnum(long num,char *print);   int main() {     long num = -1234567890;     char *print = new char;     printnum(num, print);     printf ("%s\n",print); }   void  printnum(long num, char *print ) {     long tmp;     char *arra[]= {"Zero ", "One ", "Two ","Three ", "Four ", "Five ","Six ", "Seven ", "Eight ","Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ","Fourteen ", "Fifteen ", "Sixteen ","Seventeen ",         "Eighteen ","Nineteen "};       char *arraTens[]= {"", "", "Twenty ","Thrirty ", "Fourty ", "Fifty ","Sixty ", "Seventy ", "Eighty ","Ninety "};       if(!num) // == 0     {         print= arra[num];         return ;     }       if(num < 0) // -ve     {         strcpy (print,"minus ");         num = 0 - num; //make it +ve just for reading     }       if(num > 9999999)       {         tmp = num /10000000;         printnum(tmp, print);         strcat (print,"crore ");         num  = num %10000000;     }     if(num > 99999)     {         tmp = num /100000;         printnum(tmp, print);         strcat (print,"lakh ");         num  = num %100000;     }     if(num > 999)     {         tmp = num /1000;         printnum(tmp, print);         strcat (print,"thousand ");         num  = num %1000;     }     if (num >99)     {         tmp = num /100;         strcat (print, arra[tmp]);         strcat (print,"hundred ");         num  = num %100;     }         if (num >19)     {         tmp =num - (num%10);         strcat (print, arraTens[tmp/10]);         num = num %10;     }       if ( num > 0 && num < 20 )     {         strcat (print, arra[num]);         num = 0;     }   } Mar 2 '14 #11 