program that will input a number and display it in words

Thread Solved

Join Date: Nov 2007
Posts: 26
Reputation: makubexiii is an unknown quantity at this point 
Solved Threads: 0
makubexiii's Avatar
makubexiii makubexiii is offline Offline
Light Poster

program that will input a number and display it in words

 
0
  #1
Dec 12th, 2007
im a 1st year comsci student and we are given an assignment that will display in words the number that is entered using logical operators and what we've learned so far(printf, scanf,etc.)
I only have problems in displaying the numbers from 11-19 (when I input 12 is displays two twelve), and when i got through it comes another problem with the ones digits (when I input 1 nothing comes out).
pls help..

here's the code so far..
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. int num, a,w,x,y,z;
  6. clrscr();
  7. printf("Enter a number: ");
  8. scanf("%d", &num);
  9.  
  10. a=num/1000;
  11. if (a==1)
  12. printf("one thousand");
  13. else if (a==2)
  14. printf("two thousand");
  15. else if (a==3)
  16. printf("three thousand");
  17. else if (a==4)
  18. printf("four thousand");
  19. else if (a==5)
  20. printf("five thousand");
  21. else if (a==6)
  22. printf("six thousand");
  23. else if (a==7)
  24. printf("seven thousand");
  25. else if (a==8)
  26. printf("eight thousand");
  27. else if (a==9)
  28. printf("nine thousand");
  29.  
  30. y=num%1000;
  31. if (y>=100&&y<200)
  32. printf(" one hundred");
  33. else if (y>=200&&y<300)
  34. printf(" two hundred");
  35. else if (y>=300&&y<400)
  36. printf(" three hundred");
  37. else if (y>=400&&y<500)
  38. printf(" four hundred");
  39. else if (y>=500&&y<600)
  40. printf(" five hundred");
  41. else if (y>=600&&y<700)
  42. printf(" six hundred");
  43. else if (y>=700&&y<800)
  44. printf(" seven hundred");
  45. else if (y>=800&&y<900)
  46. printf(" eight hundred");
  47. else if (y>=900&&y<1000)
  48. printf(" nine hundred");
  49.  
  50. z=num%100;
  51. if (z>=20&&z<30)
  52. printf( " twenty");
  53. else if (z>=30&&z<40)
  54. printf( " thirty");
  55. else if (z>=40&&z<50)
  56. printf( " forty");
  57. else if (z>=50&&z<60)
  58. printf( " fifty");
  59. else if (z>=60&&z<70)
  60. printf( " sixty");
  61. else if (z>=70&&z<80)
  62. printf( " seventy");
  63. else if (z>=80&&z<90)
  64. printf( " eighty");
  65. else if (z>=90&&z<100)
  66. printf( " ninety");
  67.  
  68. if (x<10&&x>20)
  69.  
  70. x=z%10;
  71. if (x==1)
  72. printf(" one");
  73. else if (x==2)
  74. printf(" two");
  75. else if (x==3)
  76. printf(" three");
  77. else if (x==4)
  78. printf(" four");
  79. else if (x==5)
  80. printf(" five");
  81. else if (x==6)
  82. printf(" six");
  83. else if (x==7)
  84. printf(" seven");
  85. else if (x==8)
  86. printf(" eight");
  87. else if (x==9)
  88. printf(" nine");
  89.  
  90. if (x>10&&x<20)
  91. { printf("");}
  92.  
  93. w=num;
  94. if (w==10)
  95. printf("ten");
  96. else if (w==11)
  97. printf(" eleven");
  98. else if (w==12)
  99. printf(" twelve");
  100. else if (w==13)
  101. printf(" thirteen");
  102. else if (w==14)
  103. printf(" fourteen");
  104. else if (w==15)
  105. printf(" fifteen");
  106. else if (w==16)
  107. printf(" sixteen");
  108. else if (w==17)
  109. printf(" seventeen");
  110. else if (w==18)
  111. printf(" eighteen");
  112. else if (w==19)
  113. printf(" nineteen");
  114.  
  115. getch();
  116. }
  117. 
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: program that will input a number and display it in words

 
0
  #2
Dec 12th, 2007
Look carefully at this section of your code:
  1. if (x<10&&x>20)
  2.  
  3. x=z%10;
  4. if (x==1)
  5. printf(" one");
  6. ...

You also might want to consider using more spaces in each line to make the code more readable. Something like:

  1. z = num % 100;
  2. if (z >= 20 && z < 30)
  3. ...
  4. else if (z >= 40 && z < 50)
  5. printf( " forty");
  6. else if (z >= 50 && z < 60)
  7. printf( " fifty");
  8. else if (z >= 60 && z < 70)
  9. printf( " sixty");
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: makubexiii is an unknown quantity at this point 
Solved Threads: 0
makubexiii's Avatar
makubexiii makubexiii is offline Offline
Light Poster

Re: program that will input a number and display it in words

 
0
  #3
Dec 12th, 2007
Originally Posted by WaltP View Post
Look carefully at this section of your code:
  1. if (x<10&&x>20)
  2.  
  3. x=z%10;
  4. if (x==1)
  5. printf(" one");
  6. ...
at first I didn't have the
  1. if (x<10&&x>20)
thats the time where the output for 12 is two twelve and 512 is five hundred two.. but when its there the ones digit are not displayed 1 is blank and 554 is five hundred fifty..
its like every time I fix one problem , another one comes out..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,839
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: program that will input a number and display it in words

 
0
  #4
Dec 12th, 2007
What WaltP is trying to say is that: if (x<10&&x>20) is wrong:
This statement means: if x is less then 10 AND bigger then 20. What number do you know that is smaller then 10 and bigger then 20?
Just replace it with : if (x<10)
And another thing: what happens when your number ends with a zero?

Originally Posted by makubexiii View Post
its like every time I fix one problem , another one comes out..
Welcome to the wonderfull world of programming

Niek
Last edited by niek_e; Dec 12th, 2007 at 8:06 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: makubexiii is an unknown quantity at this point 
Solved Threads: 0
makubexiii's Avatar
makubexiii makubexiii is offline Offline
Light Poster

Re: program that will input a number and display it in words

 
0
  #5
Dec 12th, 2007
Originally Posted by niek_e View Post
What WaltP is trying to say is that: if (x<10&&x>20) is wrong:
This statement means: if x is less then 10 AND bigger then 20. What number do you know that is smaller then 10 and bigger then 20?
Just replace it with : if (x<10)
And another thing: what happens when your number ends with a zero?


Welcome to the wonderfull world of programming

Niek
the numbers that ends in zero are fine.. 120 will just display one hundred twenty..
i think I need a code that if its between 11-19 it will display the phrase and not display the ones digit. but if its outside 11-19 it will disregard the 11-19 codes..
if i use if (x<10) it still wont display the ones digits

the world of programming sure is wonderful.. it really cracks my head..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,839
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: program that will input a number and display it in words

 
0
  #6
Dec 12th, 2007
  1. if (x<10&&x>20)
  2.  
  3. x=z%10;
I tried to run your code and came across the above. You're checking if x<10 BEFORE it is assigned a value... This will crash your program.
Change it to this:
  1. x=z%10;
  2. if (x<10)
And you're done! (well..almost anyway. Try 12 for example. This is all due to placement of if's)

clrscr() is not a standard C function not is getch(). I guess you're using the ancient turbo c compiler? Anyway, removing those lines (and conio.h) will make you're code standard C so it will compile on other compilers. Replace getch() with getchar();

regards Niek
Last edited by niek_e; Dec 12th, 2007 at 11:46 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: program that will input a number and display it in words

 
0
  #7
Dec 12th, 2007
You might also consider using arrays for your 'words'. It would make your code shorter. For example:
  1. char *number[]= { "", "one", "two", "three", "four"... "nine"};
  2. ...
  3. x = z % 10;
  4. printf( " %s", number[x]);
You can do this for all your numbers, including the teens and 10-90.
The 'zero' is blank because you never really output 0 in word form.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: makubexiii is an unknown quantity at this point 
Solved Threads: 0
makubexiii's Avatar
makubexiii makubexiii is offline Offline
Light Poster

Re: program that will input a number and display it in words

 
0
  #8
Dec 12th, 2007
Originally Posted by WaltP View Post
You might also consider using arrays for your 'words'. It would make your code shorter. For example:
  1. char *number[]= { "", "one", "two", "three", "four"... "nine"};
  2. ...
  3. x = z % 10;
  4. printf( " %s", number[x]);
You can do this for all your numbers, including the teens and 10-90.
The 'zero' is blank because you never really output 0 in word form.
the only problem with this is that it hasn't been taught to us yet (the *number[] part)..
so we have to use only whatever has been taught to us
i tried using nested if's but all those {} are making me dizzy, i keep getting errors and i cant keep track of my variables..

@niek_e
yes im using the ancient turbo c. thats what we'rt told to use..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: makubexiii is an unknown quantity at this point 
Solved Threads: 0
makubexiii's Avatar
makubexiii makubexiii is offline Offline
Light Poster

Re: program that will input a number and display it in words

 
0
  #9
Dec 13th, 2007
just got back from school..
i got the 10-19 part ok now.. before when i type 312 it says three hundred two but its fixed now..
the only problem left is that it wont display ones digits..
i've gone over reviewing this many times but i cant figure out whats wrong..
heres the code now

  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. int num, a,w,x,y,z;
  6. clrscr();
  7. printf("Enter a number: ");
  8. scanf("%d", &num);
  9.  
  10.  
  11. a=num/1000;
  12. if (a==1)
  13. printf("one thousand");
  14. else if (a == 2)
  15. printf("two thousand");
  16. else if (a== 3)
  17. printf("three thousand");
  18. else if (a == 4)
  19. printf("four thousand");
  20. else if (a == 5)
  21. printf("five thousand");
  22. else if (a==6)
  23. printf("six thousand");
  24. else if (a == 7)
  25. printf("seven thousand");
  26. else if (a == 8)
  27. printf("eight thousand");
  28. else if (a == 9)
  29. printf("nine thousand");
  30.  
  31. y=num % 1000;
  32. if (y >= 100 && y < 200)
  33. printf(" one hundred");
  34. else if (y >= 200 && y < 300)
  35. printf(" two hundred");
  36. else if (y >= 300 && y < 400)
  37. printf(" three hundred");
  38. else if (y >= 400 && y < 500)
  39. printf(" four hundred");
  40. else if (y >= 500 && y < 600)
  41. printf(" five hundred");
  42. else if (y >= 600 && y < 700)
  43. printf(" six hundred");
  44. else if (y >= 700 && y < 800)
  45. printf(" seven hundred");
  46. else if (y >= 800 && y < 900)
  47. printf(" eight hundred");
  48. else if (y >= 900 && y < 1000)
  49. printf(" nine hundred");
  50.  
  51. z=num%100;
  52. if (z >= 20 && z < 30)
  53. printf( " twenty");
  54. else if (z >= 30 && z < 40)
  55. printf( " thirty");
  56. else if (z >= 40 && z < 50)
  57. printf( " forty");
  58. else if (z >= 50 && z < 60)
  59. printf( " fifty");
  60. else if (z >= 60 && z < 70)
  61. printf( " sixty");
  62. else if (z >= 70 && z < 80)
  63. printf( " seventy");
  64. else if (z >= 80 && z < 90)
  65. printf( " eighty");
  66. else if (z >= 90 && z < 100)
  67. printf( " ninety");
  68. else if (z >= 10 && z < 20)
  69. {w = z;}
  70.  
  71. if (w == 10)
  72. printf(" ten");
  73. else if (w == 11)
  74. printf(" eleven");
  75. else if (w ==12)
  76. printf(" twelve");
  77. else if (w == 13)
  78. printf(" thirteen");
  79. else if (w == 14)
  80. printf(" fourteen");
  81. else if (w == 15)
  82. printf(" fifteen");
  83. else if (w == 16)
  84. printf(" sixteen");
  85. else if (w == 17)
  86. printf(" seventeen");
  87. else if (w == 18)
  88. printf(" eighteen");
  89. else if (w == 19)
  90. printf(" nineteen");
  91.  
  92.  
  93. if (z < 10 || z >= 20)
  94. {x = z % 10;}
  95. else if (x == 1)
  96. printf(" one");
  97. else if (x == 2)
  98. printf(" two");
  99. else if (x == 3)
  100. printf(" three");
  101. else if (x == 4)
  102. printf(" four");
  103. else if (x == 5)
  104. printf(" five");
  105. else if (x == 6)
  106. printf(" six");
  107. else if (x == 7)
  108. printf(" seven");
  109. else if (x == 8)
  110. printf(" eight");
  111. else if (x == 9)
  112. printf(" nine");
  113.  
  114. getch();
  115. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: program that will input a number and display it in words

 
0
  #10
Dec 13th, 2007
Originally Posted by makubexiii View Post
the only problem with this is that it hasn't been taught to us yet (the *number[] part)..
so we have to use only whatever has been taught to us
i tried using nested if's but all those {} are making me dizzy, i keep getting errors and i cant keep track of my variables..
Then you're probably not formatting them correctly...

Originally Posted by makubexiii View Post
just got back from school..
So?

Originally Posted by makubexiii View Post
i got the 10-19 part ok now.. before when i type 312 it says three hundred two but its fixed now..
the only problem left is that it wont display ones digits..
i've gone over reviewing this many times but i cant figure out whats wrong..
Again, look carefully at this piece of code -- before niek_e gets here and tells you what I mean
  1. if (z < 10 || z >= 20)
  2. {x = z % 10;}
  3. else if (x == 1)
  4. printf(" one");
  5. else if (x == 2)
  6. printf(" two");
  7. ...
How does this code allow you to get to printf(" one") ?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC