shadowshine57

Display english name of a given number

Feb 22nd, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. //Function name: dig1
  7. //Purpose: Prints the english version of the last digit of the original number
  8. //Parameters: The last digit of the original number
  9. //Return value: None
  10. void dig1(int a)
  11. {
  12.     if (a == 0) {
  13.         cout << endl;
  14.     }
  15.     else if (a == 1) {
  16.         cout << "ONE" << endl;
  17.     }
  18.     else if (a == 2) {
  19.         cout << "TWO" << endl;
  20.     }
  21.     else if (a == 3) {
  22.         cout << "THREE" << endl;
  23.     }
  24.     else if (a == 4) {
  25.         cout << "FOUR" << endl;
  26.     }
  27.     else if (a == 5) {
  28.         cout << "FIVE" << endl;
  29.     }
  30.     else if (a == 6) {
  31.         cout << "SIX" << endl;
  32.     }
  33.     else if (a == 7) {
  34.         cout << "SEVEN" << endl;
  35.     }
  36.     else if (a == 8) {
  37.         cout << "EIGHT" << endl;
  38.     }
  39.     else if (a == 9) {
  40.         cout << "NINE" << endl;
  41.     }
  42. }
  43. //Function name: dig2
  44. /*Purpose: Prints the english version of the 3rd digit of the original number. If the 3rd digit is 1, prints the word associated with 1 and the last
  45.  digit. Also sends the value of the last digit to dig1.*/
  46. //Parameters: The last two digits of the original number
  47. //Return value: None
  48. void dig2(int c)
  49. {
  50.     int a = c / 10;
  51.     int b = c % 10;
  52.     if (a == 0) {
  53.         dig1(b);
  54.     }
  55.     else if (a == 1) {
  56.         if (b == 0) {
  57.             cout << "TEN" << endl;
  58.         }
  59.         else if (b == 1) {
  60.             cout << "ELEVEN" << endl;
  61.         }
  62.         else if (b == 2) {
  63.             cout << "TWELVE" << endl;
  64.         }
  65.         else if (b == 3) {
  66.             cout << "THIRTEEN" << endl;
  67.         }
  68.         else if (b == 4) {
  69.             cout << "FOURTEEN" << endl;
  70.         }
  71.         else if (b == 5) {
  72.             cout << "FIFTEEN" << endl;
  73.         }
  74.         else if (b == 6) {
  75.             cout << "SIXTEEN" << endl;
  76.         }
  77.         else if (b == 7) {
  78.             cout << "SEVENTEEN" << endl;
  79.         }
  80.         else if (b == 8) {
  81.             cout << "EIGHTEEN" << endl;
  82.         }
  83.         else if (b == 9) {
  84.             cout << "NINETEEN" << endl;
  85.         }
  86.     }
  87.     else if (a == 2) {
  88.         cout << "TWENTY ";
  89.         dig1(b);
  90.     }
  91.     else if (a == 3) {
  92.         cout << "THIRTY ";
  93.         dig1(b);
  94.     }
  95.     else if (a == 4) {
  96.         cout << "FOURTY ";
  97.         dig1(b);
  98.     }
  99.     else if (a == 5) {
  100.         cout << "FIFTY ";
  101.         dig1(b);
  102.     }
  103.     else if (a == 6) {
  104.         cout << "SIXTY ";
  105.         dig1(b);
  106.     }
  107.     else if (a == 7) {
  108.         cout << "SEVENTY ";
  109.         dig1(b);
  110.     }
  111.     else if (a == 8) {
  112.         cout << "EIGHTY ";
  113.         dig1(b);
  114.     }
  115.     else if (a == 9) {
  116.         cout << "NINETY ";
  117.         dig1(b);
  118.     }
  119. }
  120. //Function name: dig3
  121. //Purpose: Prints the english version of the 2nd digit of the original number. Also sends the value of the last 2 digits to dig2.
  122. //Parameters: The last three digits of the original number
  123. //Return value: None
  124. void dig3(int c)
  125. {
  126.     int a = c / 100;
  127.     int b = c % 100;
  128.     if (a == 0) {
  129.         dig2(b);
  130.     }
  131.     else if (a == 1) {
  132.         cout << "ONE HUNDRED ";
  133.         dig2(b);
  134.     }
  135.     else if (a == 2) {
  136.         cout << "TWO HUNDRED ";
  137.         dig2(b);
  138.     }
  139.     else if (a == 3) {
  140.         cout << "THREE HUNDRED ";
  141.         dig2(b);
  142.     }
  143.     else if (a == 4) {
  144.         cout << "FOUR HUNDRED ";
  145.         dig2(b);
  146.     }
  147.     else if (a == 5) {
  148.         cout << "FIVE HUNDRED ";
  149.         dig2(b);
  150.     }
  151.     else if (a == 6) {
  152.         cout << "SIX HUNDRED ";
  153.         dig2(b);
  154.     }
  155.     else if (a == 7) {
  156.         cout << "SEVEN HUNDRED ";
  157.         dig2(b);
  158.     }
  159.     else if (a == 8) {
  160.         cout << "EIGHT HUNDRED ";
  161.         dig2(b);
  162.     }
  163.     else if (a == 9) {
  164.         cout << "NINE HUNDRED ";
  165.         dig2(b);
  166.     }
  167. }
  168. //Function name: dig4
  169. //Purpose: Prints the english version of the 1st digit of the original number. Also sends the value of the last 3 digits to dig3.
  170. //Parameters: The original number
  171. //Return value: None
  172. void dig4(int c)
  173. {
  174.     int a = c / 1000;
  175.     int b = c % 1000;
  176.     if (a == 0) {
  177.         dig3(b);
  178.     }
  179.     else if (a == 1) {
  180.         cout << "ONE THOUSAND ";
  181.         dig3(b);
  182.     }
  183.     else if (a == 2) {
  184.         cout << "TWO THOUSAND ";
  185.         dig3(b);
  186.     }
  187.     else if (a == 3) {
  188.         cout << "THREE THOUSAND ";
  189.         dig3(b);
  190.     }
  191.     else if (a == 4) {
  192.         cout << "FOUR THOUSAND ";
  193.         dig3(b);
  194.     }
  195.     else if (a == 5) {
  196.         cout << "FIVE THOUSAND ";
  197.         dig3(b);
  198.     }
  199.     else if (a == 6) {
  200.         cout << "SIX THOUSAND ";
  201.         dig3(b);
  202.     }
  203.     else if (a == 7) {
  204.         cout << "SEVEN THOUSAND ";
  205.         dig3(b);
  206.     }
  207.     else if (a == 8) {
  208.         cout << "EIGHT THOUSAND ";
  209.         dig3(b);
  210.     }
  211.     else if (a == 9) {
  212.         cout << "NINE THOUSAND ";
  213.         dig3(b);
  214.     }
  215. }
  216. int main()
  217. {
  218.     int number;
  219.     int c = 0;
  220.     while (c < 1) {
  221.         cout << "Enter a number between 0 and 9999." << endl;
  222.         cin >> number;
  223.         if (number >= 0 && number <= 9999) {
  224.             if (number == 0) {
  225.                 cout << "ZERO" << endl;
  226.                 break;
  227.             }
  228.             else if (number > 0 && number < 10) {
  229.                 dig1(number);
  230.                 break;
  231.             }
  232.             else if (number >= 10 && number < 100) {
  233.                 dig2(number);
  234.                 break;
  235.             }
  236.             else if (number >= 100 && number < 1000) {
  237.                 dig3(number);
  238.                 break;
  239.             }
  240.             else if (number >= 1000 && number < 10000) {
  241.                 dig4(number);
  242.                 break;
  243.             }
  244.         }
  245.         else {
  246.             cout << "You did not enter a number between 0 and 9999." << endl;
  247.         }
  248.  
  249.     }
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment