Q. 108. Lets accept any positive number and convert it into Words using C++ program
For Example, 1890 must be written as One Thousand Eight Hundred and Ninety.
Copy Code -->>
#include <iostream>
#include <string>
using namespace std;
const string isEMPTY = "";
const string A[] = { isEMPTY, "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ",
"Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
const string B[] = { isEMPTY, isEMPTY, "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
string number2digit(int n, string append)
{
if (n == 0) {
return isEMPTY;
}
if (n > 19) {
return B[n / 10] + A[n % 10] + append;
}
else {
return A[n] + append;
}
}
string digitToWord(long int n)
{
string res;
res = number2digit((n % 100), "");
if (n > 100 && n % 100) {
res = "and " + res;
}
res = number2digit(((n / 100) % 10), "Hundred ") + res;
res = number2digit(((n / 1000) % 100), "Thousand ") + res;
res = number2digit(((n / 100000) % 100), "Lakh, ") + res;
res = number2digit(((n / 10000000) % 100), "Crore, ") + res;
return res;
}
int main()
{
long int n;
cout <<"\nEnter any Number: ";
cin >> n;
cout << n <<" in words is: "<<digitToWord(n) ;
return 0;
}
No comments:
Post a Comment