Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- void int2words(int n);
- void digit2word(int n);
- void tens2word(int n);
- void hundreds2word(int n);
- void thousands2word(int n);
- int main()
- {
- int input;
- cout << "Number to Words Converter\n\n"
- << "Enter a number: ";
- cin >> input;
- int2words(input);
- system("pause");
- return 0;
- }
- void int2words(int n)
- {
- if (n >= 10000)
- {
- cout << "Number is too big!" << endl;
- return;
- }
- int thousands = n / 1000; // get the thousands digit
- n = n % 1000;
- thousands2word(thousands); // print
- int hundreds = n / 100; // get hundreds digit
- n = n % 100;
- hundreds2word(hundreds); // print
- int tens = n / 10; // get ones digit
- tens2word(tens); // print
- int ones = n % 10; // get ones digit
- digit2word(ones); // print
- cout << endl;
- }
- void digit2word(int n)
- {
- switch(n)
- {
- case 9:
- cout << "Nine ";
- break;
- case 8:
- cout << "Eight ";
- break;
- case 7:
- cout << "Seven ";
- break;
- case 6:
- cout << "Six ";
- break;
- case 5:
- cout << "Five ";
- break;
- case 4:
- cout << "Four ";
- break;
- case 3:
- cout << "Three ";
- break;
- case 2:
- cout << "Two ";
- break;
- case 1:
- cout << "One ";
- break;
- }
- }
- void tens2word(int n)
- {
- switch(n)
- {
- case 9:
- cout << "Ninety ";
- break;
- case 8:
- cout << "Eighty ";
- break;
- case 7:
- cout << "Seventy ";
- break;
- case 6:
- cout << "Sixty ";
- break;
- case 5:
- cout << "Fifty ";
- break;
- case 4:
- cout << "Forty ";
- break;
- case 3:
- cout << "Thirty ";
- break;
- case 2:
- cout << "Twenty ";
- break;
- case 1:
- cout << "Ten ";
- break;
- }
- }
- void hundreds2word(int n)
- {
- if (n > 0)
- {
- digit2word(n);
- cout << "Hundred ";
- }
- }
- void thousands2word(int n)
- {
- if (n > 0)
- {
- digit2word(n);
- cout << "Thousand ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement