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..

#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:

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:

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");

Look carefully at this section of your code:

if (x<10&&x>20)

x=z%10;
if (x==1)
   printf(" one");
...

at first I didn't have the

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..

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?

its like every time I fix one problem , another one comes out..

Welcome to the wonderfull world of programming :)

Edited 6 Years Ago by Nick Evan: n/a

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.. :)

if (x<10&&x>20)

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:

x=z%10;
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();

Edited 6 Years Ago by Nick Evan: n/a

You might also consider using arrays for your 'words'. It would make your code shorter. For example:

char *number[]= { "", "one", "two", "three", "four"... "nine"};
...
x = z % 10;
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.

You might also consider using arrays for your 'words'. It would make your code shorter. For example:

char *number[]= { "", "one", "two", "three", "four"... "nine"};
...
x = z % 10;
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..


yes im using the ancient turbo c. thats what we'rt told to use..:)

Edited 6 Years Ago by Nick Evan: n/a

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

#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..

Then you're probably not formatting them correctly... :icon_wink:

just got back from school..

So? :icon_razz:

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 Nick Evan gets here and tells you what I mean :icon_twisted:

if (z < 10 || z >= 20)
   {x = z % 10;}
else if (x == 1)
   printf(" one");
else if (x == 2)
   printf(" two");
...

How does this code allow you to get to printf(" one") ?

Edited 6 Years Ago by Nick Evan: n/a

Again, look carefully at this piece of code -- before niek_e gets here and tells you what I mean :icon_twisted:

if (z < 10 || z >= 20)
   {x = z % 10;}
else if (x == 1)
   printf(" one");
else if (x == 2)
   printf(" two");
...

sorry about that.. still a noob arterall..:)
thank you very much for your help!:)
I forgot all about the "else"..
changed it to

else
   {x = z % 10;}
if (x==1)
   printf(" One");
else if (x==2)
   printf(" Two");...

Again, look carefully at this piece of code -- before niek_e gets here and tells you what I mean :icon_twisted:

What WaltP was trying to say here: ....nah, just kidding :) I'll try to keep my psychic powers hidden from now on!

Niek

This question has already been answered. Start a new discussion instead.