fork download
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. string units [20] = {"","One ","Two ","Three ","Four ", "Five ","Six ","SEven ","Eight ","Nine ","Ten ","Eleven ", "Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
  6.  
  7. string tens[] = {"","","Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninty "};
  8.  
  9. string digits[] = {"","","","Hundred ","Thousand ","Thousand ","Lakh ","Lakh ","Crore "};
  10.  
  11. string getOneDigit(int val, int index) {
  12. if(val < 10) {
  13. string res = units[val];
  14. if(val != 0) {
  15. res = res + digits[index];
  16. }
  17. return res;
  18. }
  19. return "";
  20. }
  21.  
  22. string getTensDigit(int val, int index) {
  23. if(val < 10) {
  24. string res = tens[val] + digits[index];
  25. return res;
  26. }
  27. return "";
  28. }
  29.  
  30.  
  31. string getTwoDigit(int val, int index) {
  32. string res = ""; int temp = val;
  33. if(val < 20) {
  34. res = units[val] + res;
  35. }
  36. else {
  37. res = getOneDigit(val%10,0) + res;
  38. val = val /10;
  39. res = getTensDigit(val %10, 1) + res;
  40. }
  41. if(temp != 0)
  42. res = res + digits[index];
  43. return res;
  44. }
  45.  
  46.  
  47.  
  48. string convertToString(int n) {
  49. string result = "";
  50. int i = 1, r;
  51.  
  52.  
  53. while(n) {
  54. if(i != 3) {
  55. r = n %100;
  56. result = getTwoDigit(r,i) + result;
  57. n = n / 100;
  58. i = i+2;
  59. }
  60. else if(i == 3) {
  61. r = n %10;
  62. result = getOneDigit(r, i) + result;
  63. n = n / 10;
  64. i++;
  65. }
  66.  
  67. }
  68.  
  69. return result;
  70. }
  71.  
  72.  
  73. int main() {
  74. int n, t;
  75. cin>>t;
  76. while(t--) {
  77. cout<<"Entet the number to be converted: ";
  78. cin>>n;
  79. cout<<"Result : "<<convertToString(n)<<endl;
  80. }
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0s 3468KB
stdin
1
1567
stdout
Entet the number to be converted: Result : One Thousand Five Hundred Sixty SEven