Advertisement
tuldok89

int2words

Sep 22nd, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void int2words(int n);
  7. void digit2word(int n);
  8. void tens2word(int n);
  9. void hundreds2word(int n);
  10. void thousands2word(int n);
  11.  
  12. int main()
  13. {
  14.     int input;
  15.     cout << "Number to Words Converter\n\n"
  16.          << "Enter a number: ";
  17.     cin >> input;
  18.  
  19.     int2words(input);
  20.  
  21.     system("pause");
  22.     return 0;
  23. }
  24.  
  25. void int2words(int n)
  26. {
  27.     if (n >= 10000)
  28.     {
  29.         cout << "Number is too big!" << endl;
  30.         return;
  31.     }
  32.  
  33.     int thousands = n / 1000; // get the thousands digit
  34.     n = n % 1000;
  35.  
  36.     thousands2word(thousands); // print
  37.  
  38.     int hundreds = n / 100; // get hundreds digit
  39.     n = n % 100;
  40.  
  41.     hundreds2word(hundreds); // print
  42.  
  43.     int tens = n / 10; // get ones digit
  44.  
  45.     tens2word(tens); // print
  46.  
  47.     int ones = n % 10; // get ones digit
  48.  
  49.     digit2word(ones); // print
  50.  
  51.     cout << endl;
  52. }
  53.  
  54. void digit2word(int n)
  55. {
  56.     switch(n)
  57.     {
  58.     case 9:
  59.         cout << "Nine ";
  60.         break;
  61.     case 8:
  62.         cout << "Eight ";
  63.         break;
  64.     case 7:
  65.         cout << "Seven ";
  66.         break;
  67.     case 6:
  68.         cout << "Six ";
  69.         break;
  70.     case 5:
  71.         cout << "Five ";
  72.         break;
  73.     case 4:
  74.         cout << "Four ";
  75.         break;
  76.     case 3:
  77.         cout << "Three ";
  78.         break;
  79.     case 2:
  80.         cout << "Two ";
  81.         break;
  82.     case 1:
  83.         cout << "One ";
  84.         break;
  85.     }
  86. }
  87.  
  88. void tens2word(int n)
  89. {
  90.     switch(n)
  91.     {
  92.     case 9:
  93.         cout << "Ninety ";
  94.         break;
  95.     case 8:
  96.         cout << "Eighty ";
  97.         break;
  98.     case 7:
  99.         cout << "Seventy ";
  100.         break;
  101.     case 6:
  102.         cout << "Sixty ";
  103.         break;
  104.     case 5:
  105.         cout << "Fifty ";
  106.         break;
  107.     case 4:
  108.         cout << "Forty ";
  109.         break;
  110.     case 3:
  111.         cout << "Thirty ";
  112.         break;
  113.     case 2:
  114.         cout << "Twenty ";
  115.         break;
  116.     case 1:
  117.         cout << "Ten ";
  118.         break;
  119.     }
  120. }
  121.  
  122. void hundreds2word(int n)
  123. {
  124.     if (n > 0)
  125.     {
  126.         digit2word(n);
  127.         cout << "Hundred ";
  128.     }
  129. }
  130.  
  131. void thousands2word(int n)
  132. {
  133.     if (n > 0)
  134.     {
  135.         digit2word(n);
  136.         cout << "Thousand ";
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement