Mr. Stephen is working in a translator company. His task is to translate the given cash (positive integer number) into words. He found that maximum cash value cannot be more than 10000000000. Write the C++ code to help Mr. Stephen.
What I have tried:
#include <iostream>
using namespace std;
string one[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen " };
string ten[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety " };
string numToWords(int n, string s)
{
string str = "";
if (n > 19)
str += ten[n / 10] + one[n % 10];
else
str += one[n];
if (n)
str += s;
return str;
}
string convertToWords(long n)
{
string out;
out += numToWords((n / 10000000), "crore ");
out += numToWords(((n / 100000) % 100), "lakh ");
out += numToWords(((n / 1000) % 100), "thousand ");
out += numToWords(((n / 100) % 10), "hundred ");
out += numToWords(((n/100) %10), "tens");
if (n > 100 && n % 100)
out += numToWords((n % 100), "");
if(out=="")
out = "zero";
return out;
}
int main()
{
int num,temp,length=0,result,n,m=0;
cin>>num;
temp=num;
cout << convertToWords(n) << endl;
return 0;
}