#include <iostream> #include <sstream> using namespace std; string unit[] = {"","one","two","three","four","five","six","seven","eight","nine"}; string teen[] = {"","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; string ty[] = {"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; int countDigits(int n) { int count = 0; while(n) { n/= 10; count++; } return count; } string numToString(int n) { string result ; ostringstream r; r<<n; result = r.str(); return result; } void print10000(int d1,int d2) { if(d1 > 1) { cout<<ty[d1]<<" "; cout<<unit[d2]<<" "; } else { if(d2) cout<<teen[d1]<<" "; else cout<<"Ten "; } cout<<"thousand "; } void print1000(int d) { if(d) { cout<<unit[d]<<" thousand "; } } void print100(int d) { if(d) { cout<<unit[d]<<" hundred "; } } void print10(int d1,int d2) { if(d1) { if(d1 > 1) { cout<<ty[d1]<<" "; cout<<unit[d2]<<" "; } else if(d2) cout<<teen[d2]; else cout<<"Ten"; } else if(d2) { cout<<unit[d2]; } } string lengthFive(string number) { int digit5,digit4,digit3,digit2,digit1; cout<<"Number is "<<number<<"\n"; digit1 = number[4] - 48; digit2 = number[3] - 48; digit3 = number[2] - 48; digit4 = number[1] - 48; digit5 = number[0] - 48; print10000(digit5,digit4); print100(digit3); print10(digit2,digit1); } string lengthFour(string number) { int digit4 = number[0] - 48; int digit3 = number[1] - 48; int digit2 = number[2] - 48; int digit1 = number[3] - 48; print1000(digit4); print100(digit3); print10(digit2,digit1); } string lengthThree(string number) { int digit3 = number[0] - 48; int digit2 = number[1] - 48; int digit1 = number[2] - 48; print100(digit3); print10(digit2,digit1); } string lengthTwo(string number) { int digit2 = number[0] - 48; int digit1 = number[1] - 48; print10(digit2,digit1); } string lengthOne(string number) { int digit1 = number[0] - 48; cout<<unit[digit1]; } int main() { int n = 10002; if(!n) { cout<<"zero"; return 0; } int len = countDigits(n); string number = numToString(n); switch(len) { case 1: lengthOne(number);break; case 2: lengthTwo(number);break; case 3: lengthThree(number);break; case 4: lengthFour(number);break; case 5: lengthFive(number);break; default: cout<<"Invalid"; } system("pause"); }
Monday, 24 June 2013
Program to get the number and print the number in words (number <= 99999). For eg. 1234 is one thousand two hundred thirty four.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment