473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to print the numbers in words?

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 55106
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
  1. int num, temp,lastnumber[100];
  2.  
  3. num=12345;
  4. temp=num;
  5. for(int i=1;temp==0;i++)
  6. temp=temp/10;     //to check how many digits are there 
  7.                           // even if  zero is included in it like 10004
  8.                            // so number of digits is equal to i;
  9. temp=num;
  10. for(int j=1;i==j;j++)
  11. {
  12. num=num/10;   // since num is integer it will exculde the decimal
  13. num=num*10;   // exclude number will be replaced by zero
  14. lastnumber[j]=temp-num;
  15. 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
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
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. void convert(unsigned long Value);
  6.  
  7. void main(void)
  8. {
  9.     unsigned long x =1, Val=0, i=1;
  10.  
  11.     if( (x>=100)&&(x<1000) )
  12.         i=2;
  13.     else if( (x>=1000)&&(x<=99999) )
  14.         i=3;
  15.     else if( (x>=100000)&&(x<=9999999) )
  16.         i=4;
  17.     else if( (x>=10000000)&&(x<=999999999) )
  18.         i=5;
  19.  
  20.     while(i)
  21.     {
  22.         if(i==1)
  23.         {
  24.             convert(x%100);
  25.         }
  26.         else if(i==2)
  27.         {
  28.             convert((x%1000)/100);
  29.             printf("Hundred ");
  30.         }
  31.         else if(i==3)
  32.         {
  33.             convert((x%100000)/1000);
  34.             printf("Thousand ");
  35.         }
  36.         else if(i==4)
  37.         {
  38.             convert((x%10000000)/100000);
  39.             printf("Lakh ");
  40.         }
  41.         else if(i==5)
  42.         {
  43.             convert((x%1000000000)/10000000);
  44.             printf("Crore ");
  45.         }
  46.         --i;
  47.     }
  48.     printf("\n\n");
  49. }
  50.  
  51. void convert(unsigned long Value)
  52. {
  53.     unsigned long Val=0, i=0;
  54.     char Ones[10][10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};
  55.     char Teens[15][15] = {"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
  56.     char Tens[15][15] = {"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
  57.  
  58.     for(i=0;i<2;i++)
  59.     {
  60.         if(i==0)
  61.         {
  62.             Val = Value%100;
  63.             if(Val<20) i=2;
  64.         }
  65.         else if(i==1)    Val = Value%10;
  66.         if( (Val>=10)&&(Val<=19) )
  67.             printf("%s ",&Teens[Val-10][0]);
  68.         else if( (Val>=1)&&(Val<=9) )
  69.             printf("%s ",&Ones[Val-1][0]);
  70.         else if( (Val>=20)&&(Val<=99) )
  71.             printf("%s ",&Tens[((Val-(Val%10))/10)-2][0]);
  72.     }
  73. }
Jul 12 '06 #3
Expand|Select|Wrap|Line Numbers
  1. void convert(unsigned long Value)
  2. {
  3. unsigned long Val=0, i=0;
  4. char Ones[10][10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};
  5. char Teens[15][15] = {"Ten","Eleven","Tweleve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
  6. char Tens[15][15] = {"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
  7.  
  8. for(i=0;i<2;i++)
  9. {
  10. if(i==0)
  11. {
  12. Val = Value%100;
  13. if(Val<20) i=2;
  14. }
  15. else if(i==1) Val = Value%10;
  16. if( (Val>=10)&&(Val<=19) )
  17. printf("%s ",&Teens[Val-10][0]);
  18. else if( (Val>=1)&&(Val<=9) )
  19. printf("%s ",&Ones[Val-1][0]);
  20. else if( (Val>=20)&&(Val<=99) )
  21. printf("%s ",&Tens[((Val-(Val%10))/10)-2][0]);
  22. }
  23. }
-------------------------------------------------------------------------------------------------------------------
upstair, what does this code mean? can translate to me ???
Sep 1 '06 #4
------------------------------------------------------------------------------------------------------------------
int num, temp,lastnumber[100];

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
r035198x
13,262 8TB
-----------------------------------------------------------------------------


could you please explain these more...?
Which part do you not understand?
Mar 8 '07 #6
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
  1.  
  2. Code goes here, isn't this easier to read? 
;-)
Mar 8 '07 #7
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
Expand|Select|Wrap|Line Numbers
  1. /*************************************************************
  2.  program to convert given integer to words
  3.  Date:23/10/2010,Day:Saturday,Rakesh
  4. *************************************************************/
  5. #include<stdio.h>
  6. char str[100][100]={"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN",                                     "EIGHT","NINE"};
  7. char s[20][20]={"ELEVEN","TWELEVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN",
  8.                "SEVENTEEN","EIGHTEEN","NINTEEN"};
  9. char ss[10][10]={"TEN","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY",
  10.             "NINTY"};
  11. int main()
  12. {
  13.    long int num;
  14.    int n,r,c=0;
  15.    printf("Enter the number:\n");
  16.    scanf("%ld",&num);
  17.    n=num;
  18.    while(n) {
  19.     r=n%10;
  20.     n=n/10;
  21.          c++;
  22.    }
  23.    switch(c)   {
  24.     case 1:unit(num);break;
  25.     case 2:tens(num);break;
  26.     case 3:hundreds(num);break;
  27.     case 4:thousand(num);break;
  28.     case 5:ten_thousand(num);break;
  29.     case 6:lak(num);break;
  30.     case 7:ten_lak(num);break;
  31.     case 8:crore(num);break;
  32.     case 9:ten_crore(num);break;
  33.     }
  34.     printf("\n");
  35.   return 0;
  36. }
  37. int unit(int num)  {
  38.     num?printf("%s ",str[num-1]):printf("ZERO ");
  39. }
  40. int tens(int num)  {
  41.         (num>10&&num<20)?printf("%s ",s[num-11]):(printf("%s ",ss[num/10-1])
  42.     &&((num%10)?unit(num%10):1));
  43. }
  44. int hundreds(int num)  {
  45. (num/100)?(unit(num/100)&&printf("HUNDRED AND ")):1;
  46.      (num%100)?tens(num%100):1;    
  47. }
  48. int thousand(int num)  {
  49.     (num/1000)?(unit(num/1000)&&printf("THOUSAND ")):1;
  50.     (num%1000)?hundreds(num%1000):1;
  51. }
  52. int ten_thousand(int num) {
  53.     (num/1000)?(tens(num/1000)&&printf("THOUSAND ")):1;
  54.     (num%1000)?hundreds(num%1000):1;
  55. }
  56. int lak(int num) {
  57.     (num/100000)?(tens(num/10000)&&printf("LAKHS ")):1;
  58.     (num%100000)?ten_thousand(num%100000):1;    
  59. }
  60. int ten_lak(int num)  {
  61.     (num/100000)?(tens(num/100000)&&printf("LAKHS ")):1;
  62.     (num%100000)?ten_thousand(num%100000):1;
  63. }
  64. int crore(int num)  {
  65.     (num/10000000)?(tens(num/10000000)&&printf("CRORE ")):1;
  66.        (num%1000000)?ten_lak(num%10000000):1;
  67. }
  68. int ten_crore(int num)  {
  69.     (num/10000000)?(tens(num/10000000)&&printf("CRORE ")):1;
  70.     (num%10000000)?ten_lak(num%10000000):1;
  71.             }
Oct 28 '10 #9
Here's my Code its C..programming..

Expand|Select|Wrap|Line Numbers
  1. #include <conio.h> 
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. char *make_words(char *s, int ncomma), *insert_comma(long n, int *ncomma),*int2words(int n);
  6. int a;
  7. main(void)
  8. {printf("\nEnter number to be converted to words: ");
  9.  scanf("%d",&a);
  10.  printf("\n\nEquivalent in words is:\n\n   \t\t\t%s ! \n",int2words(a));
  11. getch();  
  12. return 0;}
  13. char *make_words(char *s, int ncomma)
  14. {
  15.   int i, len, rest = 0;
  16.   char *p = NULL;
  17.   static char zzz[256];
  18.   static char *ones[] = {"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
  19.   static char *tens[] = {"Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
  20.   static char *twenties[] = {"","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
  21.   static char *hundreds[] = {"Hundred ","Thousand ","Million "};
  22. memset(zzz, '\0', 256);
  23. len = strlen(s);
  24. for(i = 0; i < len; i++)
  25. {if ((p = strchr((s[i] == ',') ? &s[++i] : &s[i], ',')) == NULL){
  26. p = &s[strlen(s)];}
  27. if (s[i] == '0'){
  28. continue;}
  29. if ((rest = (p - &s[i])) != 0){
  30. if (rest == 3){
  31. strcat(zzz, ones[s[i] - '0' - 1]);
  32. strcat(zzz, hundreds[0]);
  33. if (len == 7 && s[2] == '0')  strcat(zzz, hundreds[1]);
  34. if (len == 11 && s[2] == '0')  strcat(zzz, hundreds[2]);}
  35. else if (rest == 2) {
  36. if (s[i] == '1'){
  37. strcat(zzz, tens[s[++i] - '0']);
  38. rest--;}
  39. else{
  40. strcat(zzz, twenties[s[i] - '0' - 1]);}}
  41. else
  42. strcat(zzz, ones[s[i] - '0' - 1]);}
  43. if (rest == 1 && ncomma != 0){
  44. strcat(zzz, hundreds[ncomma--]);}}
  45. return zzz;}
  46. char *insert_comma(long n, int *ncomma){
  47. static char zzz[30];
  48. int i = 0;
  49. char *p = &zzz[sizeof(zzz)-1];
  50. *p = '\0';
  51. *ncomma = 0;
  52. do {
  53. if (i % 3 == 0 && i != 0) {
  54. *--p = ',';
  55. ++*ncomma;}
  56. *--p = (char)('0' + n % 10);
  57. n /= 10;
  58. i++;}
  59. while(n != 0);
  60. return p;}
  61. char *int2words(int n){
  62. int nc;
  63. char *ps, *zzz, *minus;
  64. char *buffer, *v;
  65. buffer = (char *) malloc(256);
  66. if (n <= 0){
  67. minus = " Zero";
  68. n = abs(n);}
  69. else{
  70. minus = "";}
  71. ps = insert_comma(n, &nc);
  72. zzz = make_words(ps, nc);
  73. sprintf(buffer,"%s %s", minus, zzz);
  74. return buffer;
  75. getch();
  76. return 0;}
  77.  
Good Luck!
Jan 30 '11 #10
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5.  
  6. void printnum(long num,char *print);
  7.  
  8. int main()
  9. {
  10.     long num = -1234567890;
  11.     char *print = new char[1024];
  12.     printnum(num, print);
  13.     printf ("%s\n",print);
  14. }
  15.  
  16. void  printnum(long num, char *print )
  17. {
  18.     long tmp;
  19.     char *arra[]= {"Zero ", "One ", "Two ","Three ", "Four ", "Five ","Six ", "Seven ", "Eight ","Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ","Fourteen ", "Fifteen ", "Sixteen ","Seventeen ",
  20.         "Eighteen ","Nineteen "};
  21.  
  22.     char *arraTens[]= {"", "", "Twenty ","Thrirty ", "Fourty ", "Fifty ","Sixty ", "Seventy ", "Eighty ","Ninety "};
  23.  
  24.     if(!num) // == 0
  25.     {
  26.         print= arra[num];
  27.         return ;
  28.     }
  29.  
  30.     if(num < 0) // -ve
  31.     {
  32.         strcpy (print,"minus ");
  33.         num = 0 - num; //make it +ve just for reading
  34.     }
  35.  
  36.     if(num > 9999999)
  37.  
  38.     {
  39.         tmp = num /10000000;
  40.         printnum(tmp, print);
  41.         strcat (print,"crore ");
  42.         num  = num %10000000;
  43.     }
  44.     if(num > 99999)
  45.     {
  46.         tmp = num /100000;
  47.         printnum(tmp, print);
  48.         strcat (print,"lakh ");
  49.         num  = num %100000;
  50.     }
  51.     if(num > 999)
  52.     {
  53.         tmp = num /1000;
  54.         printnum(tmp, print);
  55.         strcat (print,"thousand ");
  56.         num  = num %1000;
  57.     }
  58.     if (num >99)
  59.     {
  60.         tmp = num /100;
  61.         strcat (print, arra[tmp]);
  62.         strcat (print,"hundred ");
  63.         num  = num %100;
  64.     }
  65.  
  66.  
  67.     if (num >19)
  68.     {
  69.         tmp =num - (num%10);
  70.         strcat (print, arraTens[tmp/10]);
  71.         num = num %10;
  72.     }
  73.  
  74.     if ( num > 0 && num < 20 )
  75.     {
  76.         strcat (print, arra[num]);
  77.         num = 0;
  78.     }
  79.  
  80. }
Mar 2 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Colin McGuire | last post by:
Hi, I have an report in Microsoft Access and it displays everything in the table. One column called "DECISION" in the table has either 1,2, or 3 in it. On my report it displays 1, 2, or 3. I want...
24
by: Rhino | last post by:
I am dabbling with print CSS for the first time and I need some guidance. The web pages on my site look fine - to my untrained eye - when displayed on the monitor in any of the standard browsers....
3
by: isaac86 | last post by:
how to print out d words that have length 7-10? printf ( "%u characters long for %s", strlen( data)-1,data ); wLength = strlen( data)-1; if(wLength == 7||8||9||10){ ...
1
by: qwertz | last post by:
Dear All; I Am New To Perl Not So Experienced::::i Would Like To Know How I Can Compare Two Different Hashes And Print Thecommon Values In Them::it Would Be Great If Simeone Could Help Me Thanks...
2
by: laith kawar | last post by:
Hey everybody, i need a function that takes a number and prints the reverse. for example input 321, output 123
8
by: andrew.smith.cpp | last post by:
Hello how Can i get this kind of output? 1,2,3,5,8,13,21 i try it with loop but could print it. Thanks
21
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated dynamically. I have ran this program with a file...
4
by: lightning18 | last post by:
I have a list of incorrect words called # words and another list containing my txt file # text. I want to print the line number of the words located in the text. I get the following error for my...
1
by: deneme birki | last post by:
hello. i want to print one of random words that i defined before, on screen. but using an external .txt or .dat file. for example i add 100 words on the words.txt or words.dat file and program will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.