| | |
program that will input a number and display it in words
Thread Solved |
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..
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..
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> main() { int num, a,w,x,y,z; clrscr(); printf("Enter a number: "); scanf("%d", &num); a=num/1000; if (a==1) printf("one thousand"); else if (a==2) printf("two thousand"); else if (a==3) printf("three thousand"); else if (a==4) printf("four thousand"); else if (a==5) printf("five thousand"); else if (a==6) printf("six thousand"); else if (a==7) printf("seven thousand"); else if (a==8) printf("eight thousand"); else if (a==9) printf("nine thousand"); y=num%1000; if (y>=100&&y<200) printf(" one hundred"); else if (y>=200&&y<300) printf(" two hundred"); else if (y>=300&&y<400) printf(" three hundred"); else if (y>=400&&y<500) printf(" four hundred"); else if (y>=500&&y<600) printf(" five hundred"); else if (y>=600&&y<700) printf(" six hundred"); else if (y>=700&&y<800) printf(" seven hundred"); else if (y>=800&&y<900) printf(" eight hundred"); else if (y>=900&&y<1000) printf(" nine hundred"); z=num%100; if (z>=20&&z<30) printf( " twenty"); else if (z>=30&&z<40) printf( " thirty"); else if (z>=40&&z<50) printf( " forty"); else if (z>=50&&z<60) printf( " fifty"); else if (z>=60&&z<70) printf( " sixty"); else if (z>=70&&z<80) printf( " seventy"); else if (z>=80&&z<90) printf( " eighty"); else if (z>=90&&z<100) printf( " ninety"); if (x<10&&x>20) x=z%10; if (x==1) printf(" one"); else if (x==2) printf(" two"); else if (x==3) printf(" three"); else if (x==4) printf(" four"); else if (x==5) printf(" five"); else if (x==6) printf(" six"); else if (x==7) printf(" seven"); else if (x==8) printf(" eight"); else if (x==9) printf(" nine"); if (x>10&&x<20) { printf("");} w=num; if (w==10) printf("ten"); else if (w==11) printf(" eleven"); else if (w==12) printf(" twelve"); else if (w==13) printf(" thirteen"); else if (w==14) printf(" fourteen"); else if (w==15) printf(" fifteen"); else if (w==16) printf(" sixteen"); else if (w==17) printf(" seventeen"); else if (w==18) printf(" eighteen"); else if (w==19) printf(" nineteen"); getch(); }
Look carefully at this section of your code:
You also might want to consider using more spaces in each line to make the code more readable. Something like:
C Syntax (Toggle Plain Text)
if (x<10&&x>20) x=z%10; if (x==1) printf(" one"); ...
You also might want to consider using more spaces in each line to make the code more readable. Something like:
C Syntax (Toggle Plain Text)
z = num % 100; if (z >= 20 && z < 30) ... else if (z >= 40 && z < 50) printf( " forty"); else if (z >= 50 && z < 60) printf( " fifty"); else if (z >= 60 && z < 70) 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
Look carefully at this section of your code:
C Syntax (Toggle Plain Text)
if (x<10&&x>20) x=z%10; if (x==1) printf(" one"); ...
C Syntax (Toggle Plain Text)
if (x<10&&x>20)
its like every time I fix one problem , another one comes out..
What WaltP is trying to say is that:
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 :
And another thing: what happens when your number ends with a zero?
Welcome to the wonderfull world of programming 
Niek
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?
•
•
•
•
its like every time I fix one problem , another one comes out..

Niek
Last edited by niek_e; Dec 12th, 2007 at 8:06 am.
•
•
•
•
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
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 digitsthe world of programming sure is wonderful..
it really cracks my head..
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:
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
Change it to this:
c Syntax (Toggle Plain Text)
x=z%10; if (x<10)
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.
You might also consider using arrays for your 'words'. It would make your code shorter. For example:
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.
c Syntax (Toggle Plain Text)
char *number[]= { "", "one", "two", "three", "four"... "nine"}; ... x = z % 10; printf( " %s", number[x]);
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
You might also consider using arrays for your 'words'. It would make your code shorter. For example:
You can do this for all your numbers, including the teens and 10-90.c Syntax (Toggle Plain Text)
char *number[]= { "", "one", "two", "three", "four"... "nine"}; ... x = z % 10; printf( " %s", number[x]);
The 'zero' is blank because you never really output 0 in word form.
*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..
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
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
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> main() { int num, a,w,x,y,z; clrscr(); printf("Enter a number: "); scanf("%d", &num); a=num/1000; if (a==1) printf("one thousand"); else if (a == 2) printf("two thousand"); else if (a== 3) printf("three thousand"); else if (a == 4) printf("four thousand"); else if (a == 5) printf("five thousand"); else if (a==6) printf("six thousand"); else if (a == 7) printf("seven thousand"); else if (a == 8) printf("eight thousand"); else if (a == 9) printf("nine thousand"); y=num % 1000; if (y >= 100 && y < 200) printf(" one hundred"); else if (y >= 200 && y < 300) printf(" two hundred"); else if (y >= 300 && y < 400) printf(" three hundred"); else if (y >= 400 && y < 500) printf(" four hundred"); else if (y >= 500 && y < 600) printf(" five hundred"); else if (y >= 600 && y < 700) printf(" six hundred"); else if (y >= 700 && y < 800) printf(" seven hundred"); else if (y >= 800 && y < 900) printf(" eight hundred"); else if (y >= 900 && y < 1000) printf(" nine hundred"); z=num%100; if (z >= 20 && z < 30) printf( " twenty"); else if (z >= 30 && z < 40) printf( " thirty"); else if (z >= 40 && z < 50) printf( " forty"); else if (z >= 50 && z < 60) printf( " fifty"); else if (z >= 60 && z < 70) printf( " sixty"); else if (z >= 70 && z < 80) printf( " seventy"); else if (z >= 80 && z < 90) printf( " eighty"); else if (z >= 90 && z < 100) printf( " ninety"); else if (z >= 10 && z < 20) {w = z;} if (w == 10) printf(" ten"); else if (w == 11) printf(" eleven"); else if (w ==12) printf(" twelve"); else if (w == 13) printf(" thirteen"); else if (w == 14) printf(" fourteen"); else if (w == 15) printf(" fifteen"); else if (w == 16) printf(" sixteen"); else if (w == 17) printf(" seventeen"); else if (w == 18) printf(" eighteen"); else if (w == 19) printf(" nineteen"); if (z < 10 || z >= 20) {x = z % 10;} else if (x == 1) printf(" one"); else if (x == 2) printf(" two"); else if (x == 3) printf(" three"); else if (x == 4) printf(" four"); else if (x == 5) printf(" five"); else if (x == 6) printf(" six"); else if (x == 7) printf(" seven"); else if (x == 8) printf(" eight"); else if (x == 9) printf(" nine"); getch(); }
•
•
•
•
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..
So?
•
•
•
•
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..
C Syntax (Toggle Plain Text)
if (z < 10 || z >= 20) {x = z % 10;} else if (x == 1) printf(" one"); else if (x == 2) printf(" two"); ...
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Variable scope problem (C++)
- Assembler n00b. Need Help Please. (Assembly)
- Is there any more Documenation that I need? (C++)
- Program help (Java)
- character count (C++)
- Display Longest and Shortest Word in a String (C++)
- Source Code that don't work? (Java)
- Help, record arrays (VB.NET)
- YaBasic: The beginners choice (Computer Science)
Other Threads in the C Forum
- Previous Thread: using strstr to find words
- Next Thread: cryptography .. handling large integers
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling segmentationfault send shape socket socketprograming socketprogramming stack standard string suggestions systemcall test unix urboc user voidmain() wab win32api windows.h






