arrays realted

i need to write a program to accept a number from user and print it in words, like 4563 should be printed as four five six three.
how do i do this ?
It would be most simple to accept string instead of number (so that you don't have to figure out yourself), but I'm not sure if your tutor will like that.

To extract digits out of a number x, use operators / and %. x%10 is the last digit, x/10 is the whole number except the last. I'll leave the whole algorithm for you to figure out.

Either way you do it, you'll need to define const char* digit[] = {"zero", "one", "two", ...
Instead of cout << some_digit, write cout << digit[some_digit]. The rest is just as hard as printing a number in decimal.
thanks bro, so i should %10 to get the ones place then divide by 10 to get the other numbers, but how do i seprate them ?
Last edited on
first digit = x1%10
x2 = x1/10
second digit = x2%10
x3 = x2/10
...
until x becomes 0.
You can do it in a loop, or, even better, in a recursive function.
Topic archived. No new replies allowed.