can any one knows how to convert a number to word like this
192 to one hundred ninety two

i can't get the logic of that who can suggest of that i can't solve that.. plz help me

Recommended Answers

All 20 Replies

why don't you break the number up into hundreds, tens and ones using '/' and '%', then use those to get your words from an array of words, for example.

void disp(int num){
   
   if(num == 0){
      cout << "Xero" << endl;
      return;
      }
      
   const char *onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
   const char *tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
   
   int hundreds = num / 100;
   int tens = (num / 10) % 10;
   int ones = num %10;

   if(hundreds > 0)
      cout << onesWord[hundreds] << " hundred ";
      
   if(tens > 0)
      cout << tensWord[tens] << " ";
      
   if(ones > 0)
      cout << onesWord[ones];
   
   cout << endl;
   }

how about in trillion>? and please will u make me sample of that coz i will use it with the 1 = I
4 = IV
number to roman numeral plzzzzzz

Well, for a trillion, you will need to extend the above code somewhat.

and please will u make me sample of that coz ...

how about you give it a try first...

const char *tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

can i ask what is *tensWord[]? i think ur wrong in that part i think it's
const char *tensWord[][10] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
and it is right to use char only? why do you use const>?

and how about the 148 or any that has no exact pattern in one tens hundreds.??

who can make me a code of my problemo

const char *tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

can i ask what is *tensWord[]? i think ur wrong in that part i think it's
const char *tensWord[][10] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
and it is right to use char only? why do you use const>?

You use const in this situation because the strings aren't going to change, you want them to remain constant.

The char * is an object type in C++ that literally means "pointer to char". The char *tensWord[] is a pointer to a char[].

and how about the 148 or any that has no exact pattern in one tens hundreds.??

The provided code will be close if not spot on for solving your problem here. It should print out "one hundred forty eight" because dougy83 has split the number into hundreds, tens and ones and then printed the result for each.

owh i get it but hwo about in million
can u make me some codes of that like this
#include <iostream.h>
main()
blah blah
blahblahblah

owh i get it but hwo about in million

For that you need to make more arrays,

can u make me some codes of that like this
#include <iostream.h>
main()
blah blah
blahblahblah

This is lazyness, but i have nothing to do atm so....

#include <cstdlib>
#include <iostream>

using namespace std;
const char* onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
const char* tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

void disp(int num)
{
   
   if(num == 0)
   {
         cout << "Zero" << endl;
         return;
   }
   
   if(num > 9999)
   {
         cout << "Number Out of Bounds, Please Try Again" << endl;
         return;
   }
   
   int thousands = num / 1000;
   int hundreds = num / 100;
   int tens = (num / 10) % 10;
   int ones = num %10;

   if(thousands > 0)
      cout << onesWord[thousands] << " Thousand ";

   if(hundreds > 0)
      cout << onesWord[hundreds] << " Hundred ";
      
   if(tens > 0)
      cout << tensWord[tens] << " ";
      
   if(ones > 0)
      cout << onesWord[ones];
   
   cout << endl;
   }


int main(int argc, char *argv[])
{
    cout << "Please Enter a Number\n";
    int entered = 0;
    cin >> entered;
    cout << "The Number Is: ";    
    disp(entered);
    system("PAUSE");
    return EXIT_SUCCESS;
}

just look how new cases are added to dougy83's code....

commented: If you have nothing to do, giving away homework is a poor use of your time, and a disservice to the poster -2
#include <cstdlib.h>
#include <iostream.h>

using namespace std;
const char* onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
const char* tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

void disp(int num)
{
   
   if(num == 0)
   {
         cout << "Zero" << endl;
         return;
   }
   
   if(num > 9999)
   {
         cout << "Number Out of Bounds, Please Try Again" << endl;
         return;
   }
   
   int thousands = num / 1000;
   int hundreds = num / 100;
   int tens = (num / 10) % 10;
   int ones = num %10;

   if(thousands > 0)
      cout << onesWord[thousands] << " Thousand ";

   if(hundreds > 0)
      cout << onesWord[hundreds] << " Hundred ";
      
   if(tens > 0)
      cout << tensWord[tens] << " ";
      
   if(ones > 0)
      cout << onesWord[ones];
   
   cout << endl;
   }


int main(int argc, char *argv[])
{
    cout << "Please Enter a Number\n";
    int entered = 0;
    cin >> entered;
    cout << "The Number Is: ";    
    disp(entered);
    system("PAUSE");
    return EXIT_SUCCESS;
}

i can't run this their is plenty of errors coz using namestd; is not available in My turbo C and their is no cstdli.h also help me plz

commented: Way too many posts just asking for complete code. +0

can i ask what is *tensWord[]? i think ur wrong in that part i think it's

I'm not wrong there.. But you could have picked me up on ignoring the teens (eleven..ninteen) - but you missed your chance for that.

The char * is an object type in C++ that literally means "pointer to char". The char *tensWord[] is a pointer to a char[].

That's not right! char *tensWord[] is an array of pointers to char. As I initiallised it above, it just so happens that the char being pointed to is at the start of a string/array of characters.

As I said, to get "thousands", "millions", etc displayed, you can extend the above code. Here's an example:

void disp(int num){
   static int level = 0;
   
   if(num == 0){           // this case is easy!!
      cout << "Xero" << endl;
      return;
      }
   
   if(num < 0){            // deal with negative sign
      cout << "negative ";
      num = -num;          // easier to work with positive numbers
      }
   
   // some strings we will be using
   const char *onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
   const char *tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
   const char *thousWord[] = {"", "thousand", "million", "billion", "trillion", "quadrillion", "brazillion"};
   
   // check if we should display the brazillions, billions, etc. before we
   // display the lower 0-999
   if(num > 1000){
      level++;
      disp(num / 1000);    // display the billion/million/thousand part as required
      level--;
      }
      
   int numLim = num % 1000;   // we'll just deal with the lower 0-999 of the num
   int hundreds = numLim / 100;
   if(hundreds > 0)
      cout << onesWord[hundreds] << " hundred ";
   
   int tensOnes = numLim % 100;     // lower 0-99
   if(tensOnes > 0 && tensOnes < 20){  // deal with the teens (and units)
      cout << onesWord[tensOnes] << " ";
      }
   else if(tensOnes > 0){              // deal with tens >= 20
      cout << tensWord[tensOnes/10] << " ";
      
      int ones = tensOnes % 10;     // lower 0-9
      if(ones > 0){
         cout << onesWord[ones] << " ";
         }
      }

   if(numLim > 0){                  // display the "billion"/"million"/"thousand" if there was some
      cout << thousWord[level] << " ";
      }
   
   if(level == 0){                  // display an endline when we've finished (top level)
      cout << endl;
      }
   }
commented: Stop giving homework to a lazy person that just wants a handout -2

so what is the complete of that code...? and how about in roman numbers...? tnx

#include <cstdlib.h>
#include <iostream.h>

using namespace std;
const char* onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
const char* tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};

void disp(int num)
{
   
   if(num == 0)
   {
         cout << "Zero" << endl;
         return;
   }
   
   if(num > 9999)
   {
         cout << "Number Out of Bounds, Please Try Again" << endl;
         return;
   }
   
   int thousands = num / 1000;
   int hundreds = num / 100;
   int tens = (num / 10) % 10;
   int ones = num %10;

   if(thousands > 0)
      cout << onesWord[thousands] << " Thousand ";

   if(hundreds > 0)
      cout << onesWord[hundreds] << " Hundred ";
      
   if(tens > 0)
      cout << tensWord[tens] << " ";
      
   if(ones > 0)
      cout << onesWord[ones];
   
   cout << endl;
   }


int main(int argc, char *argv[])
{
    cout << "Please Enter a Number\n";
    int entered = 0;
    cin >> entered;
    cout << "The Number Is: ";    
    disp(entered);
    system("PAUSE");
    return EXIT_SUCCESS;
}

i can't run this their is plenty of errors coz using namestd; is not available in My turbo C and their is no cstdli.h also help me plz

Thats cause i used C++ (tested on Dev-Cpp)

so what is the complete of that code...? and how about in roman numbers...? tnx

your really starting to sound like you want someone to do this for you with no effort... everything thing you need is there, you just need to add more cases, as for roman numerals, you'd need to add a flag(bool) for this and define a char array of roman numerals, and follow the same sort of method(will be a little more complex due to the how roman numerals work)

owh i get it but hwo about in million
can u make me some codes of that like this

Can't you write ANY code at all? Do we all need to do your homework for you?

This is lazyness, but i have nothing to do atm so....

"... so I'll write it for you so you can pass you class and not have to learn anything but how to beg for answers." Is this basically what you meant?

your really starting to sound like you want someone to do this for you with no effort...

Starting? I thought his second post showed this. And he confirmed it absolutely with

so what is the complete of that code...? and how about in roman numbers...? tnx

So far he has posted absolutely no code and has gotten at least 3 programs in response. C'mon guys, this is not a handout clinic here!

That's not right! char *tensWord[] is an array of pointers to char. As I initiallised it above, it just so happens that the char being pointed to is at the start of a string/array of characters.

Sorry dougy83, you are correct, and thanks for helping my understanding. :)

Can't you write ANY code at all? Do we all need to do your homework for you?


"... so I'll write it for you so you can pass you class and not have to learn anything but how to beg for answers." Is this basically what you meant?

not what i meant, but same effect, i will be more mindful next time :)

Starting? I thought his second post showed this. And he confirmed it absolutely with(...)

Yes, but I not going to be pushing people around on a forum where I'm new and not someone of power, but yes next time i will refuse...

So far he has posted absolutely no code and has gotten at least 3 programs in response. C'mon guys, this is not a handout clinic here!

And bad English with a lack of details (like Turbo C? this is the C++ forum...), doesn't help either...

#include <conio.h>
#include <iostream.h>
#include <process.h>
void disp(int num){
   static int level = 0;
   if(num == 0){
      cout << "Xero" << endl;
      return;
      }
   if(num < 0){
	 cout << "negative ";
      num = -num;
      }
   const char *onesWord[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
   const char *tensWord[] = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
   const char *thousWord[] = {"", "thousand", "million", "billion", "trillion", "quadrillion", "brazillion"};

   if(num > 1000){
      level++;
      disp(num / 1000);
      level--;
      }
   int numLim = num % 1000;
   int hundreds = numLim / 100;
   if(hundreds > 0)
      cout << onesWord[hundreds] << " hundred ";
   int tensOnes = numLim % 100;
   if(tensOnes > 0 && tensOnes < 20){
      cout << onesWord[tensOnes] << " ";
      }
   else if(tensOnes > 0){
      cout << tensWord[tensOnes/10] << " ";
      int ones = tensOnes % 10;
      if(ones > 0){
	 cout << onesWord[ones] << " ";
 }
      }
   if(numLim > 0){
      cout << thousWord[level] << " ";
      }

   if(level == 0){
      cout << endl;
      }
   }
int main(int argc, char *argv[])
{  clrscr();
 int entered = 0;
   print("Enter a Number: ");
    cin >> entered;

   print("The converted word of this number is ");

   disp(entered);
    system("PAUSE");
    getch();
    return 0;
}

ok. So you got it working I take it, good.

Technogeek_42, Can you explain exactly what it is doing, and how it works?

How can it display the "thousands"/"millions"/etc. without using any for( ; ; ) loops?

ow no my code is not complete how can i use the millions/billions.....

dougy.... ahm i think it to long to make that sorry but i can't

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.