In: Computer Science
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only)
**Please only use #include <iostream> and switch and if-else statements only. Thank you.
Ex.
Enter a number: 68954
Sixty Eight Thousand Nine Hundred Fifty Four
Enter a number: 100000
One Hundred Thousand
Enter a number: -2
Number should be from 0-1000000 only
Code -
#include <iostream>
using namespace std;
//string stored from 1-19
string singleTwoDigitWords[] = { "", "One ", "Two ", "Three ",
"Four ",
"Five ", "Six ", "Seven ", "Eight ",
"Nine ", "Ten ", "Eleven ", "Twelve ",
"Thirteen ", "Fourteen ", "Fifteen ",
"Sixteen ", "Seventeen ", "Eighteen ",
"Nineteen "
};
//storing ten multiple
string tenMultipleWords[] = { "", "", "Twenty ", "Thirty ", "Forty
",
"Fifty ", "Sixty ", "Seventy ", "Eighty ",
"Ninety "
};
string convertNumberToWords(int num, string val)
{
string str = "";
//check if the number is more than 19
if (num > 19)
str += tenMultipleWords[num / 10] +
singleTwoDigitWords[num % 10];
else
str +=
singleTwoDigitWords[num];
//if number is not zero than concatenate string
if (num)
str += val;
return str;
}
//function to covert the given integer number to words
string converNumberToWords(long num)
{
string inWords;
//if number is divisible by 10000000 crore will added
inWords += convertNumberToWords((num / 10000000),
"Crore ");
//if number is divisible by 100000 lakh will added
inWords += convertNumberToWords(((num / 100000) %
100), "Lakh ");
//if number is divisible by 1000 Thousand will added
inWords += convertNumberToWords(((num / 1000) % 100),
"Thousand ");
//if number is divisible by 100 Hundred will added
inWords += convertNumberToWords(((num / 100) % 10), "Hundred ");
if (num > 100 && num % 100)
inWords += "and ";
inWords += convertNumberToWords((num % 100), "");
return inWords;
}
int main()
{
cout<<"Enter a number: "<<endl;
int num;
cin>>num;
if(num<0){
cout<<"Number should be from 0-1000000
only"<<endl;
}
else{
//method to convert the given number to words
cout << converNumberToWords(num) <<
endl;
}
return 0;
}
Screenhshots-