number into words sample code


SUBMITTED BY: Guest

DATE: Aug. 24, 2012, 11:57 a.m.

FORMAT: C++

SIZE: 3.8 kB

HITS: 1770

  1. Convert a number into words
  2. #include <conio.h> // For getch() function only
  3. #include <iostream>
  4. using namespace std;
  5. void numword1(int);
  6. void numword2(int);
  7. int main()
  8. {
  9. long unsigned int number,temp;
  10. int mult,i,digit,digits,last_two,hundred,thousand,lakh,crore;
  11. digits=last_two=hundred=thousand=lakh=crore=0;
  12. cout<<"Enter a number(lesser than 99,99,99,999)
  13. ";
  14. cin>>number;
  15. if(number>999999999)
  16. {
  17. cout<<"Number out of range!";
  18. getch();
  19. exit(0);
  20. }
  21. if(number==0)
  22. {
  23. cout<<"Zero";
  24. getch();
  25. exit(0);
  26. }
  27. temp=number;
  28. digit=number%10; // Extracting last two digts
  29. last_two=digit;
  30. number=number/10;
  31. digit=number%10;
  32. last_two=(digit*10)+last_two;
  33. number=number/10; // Extract hundreds
  34. digit=number%10;
  35. hundred=digit;
  36. number=number/10; // Extract thousands
  37. digit=number%10;
  38. thousand=digit;
  39. number=number/10;
  40. digit=number%10;
  41. thousand=(digit*10)+thousand;
  42. number=number/10; // Extract lakhs
  43. digit=number%10;
  44. lakh=digit;
  45. number=number/10;
  46. digit=number%10;
  47. lakh=(digit*10)+lakh;
  48. number=number/10; // Extract crores
  49. digit=number%10;
  50. crore=digit;
  51. number=number/10;
  52. digit=number%10;
  53. crore=(digit*10)+crore;
  54. while(temp!=0) // Calculate number of digits in the number
  55. {
  56. temp=temp/10;
  57. digits++;
  58. }
  59. cout<<"The number in words is:
  60. ";
  61. // Printing the number in words
  62. if(digits>=8)
  63. {
  64. numword2(crore);
  65. cout<<"crores ";
  66. }
  67. if(digits>=6)
  68. {
  69. if(lakh!=0)
  70. {
  71. numword2(lakh);
  72. cout<<"lakh ";
  73. }
  74. }
  75. if(digits>=4)
  76. {
  77. if(thousand!=0)
  78. {
  79. numword2(thousand);
  80. cout<<"Thousand ";
  81. }
  82. }
  83. if(digits>=3)
  84. {
  85. if(hundred!=0)
  86. {
  87. numword2(hundred);
  88. cout<<"Hundred ";
  89. }
  90. }
  91. numword2(last_two);
  92. getch();
  93. return 0;
  94. }
  95. void numword1(int num)
  96. {
  97. switch(num)
  98. {
  99. case 0: break;
  100. case 1: cout<<"One ";
  101. break;
  102. case 2: cout<<"Two ";
  103. break;
  104. case 3: cout<<"Three ";
  105. break;
  106. case 4: cout<<"Four ";
  107. break;
  108. case 5: cout<<"Five ";
  109. break;
  110. case 6: cout<<"Six ";
  111. break;
  112. case 7: cout<<"Seven ";
  113. break;
  114. case 8: cout<<"Eight ";
  115. break;
  116. case 9: cout<<"Nine ";
  117. break;
  118. case 10: cout<<"Ten ";
  119. break;
  120. case 11: cout<<"Eleven ";
  121. break;
  122. case 12: cout<<"Twelve ";
  123. break;
  124. case 13: cout<<"Thirteen ";
  125. break;
  126. case 14: cout<<"Fourteen ";
  127. break;
  128. case 15: cout<<"Fifteen ";
  129. break;
  130. case 16: cout<<"Sixteen ";
  131. break;
  132. case 17: cout<<"Seventeen ";
  133. break;
  134. case 18: cout<<"Eighteen ";
  135. break;
  136. case 19: cout<<"Nineteen ";
  137. break;
  138. }
  139. return;
  140. }
  141. void numword2(int num)
  142. {
  143. if(num>=90)
  144. {
  145. cout<<"Ninety ";
  146. numword1(num-90);
  147. }
  148. else if(num>=80)
  149. {
  150. cout<<"Eighty ";
  151. numword1(num-80);
  152. }
  153. else if(num>=70)
  154. {
  155. cout<<"Seventy ";
  156. numword1(num-70);
  157. }
  158. else if(num>=60)
  159. {
  160. cout<<"Sixty ";
  161. numword1(num-60);
  162. }
  163. else if(num>=50)
  164. {
  165. cout<<"Fifty ";
  166. numword1(num-50);
  167. }
  168. else if(num>=40)
  169. {
  170. cout<<"Fourty ";
  171. numword1(num-40);
  172. }
  173. else if(num>=30)
  174. {
  175. cout<<"Thirty ";
  176. numword1(num-30);
  177. }
  178. else if(num>=20)
  179. {
  180. cout<<"Twenty ";
  181. numword1(num-20);
  182. }
  183. else
  184. numword1(num);
  185. return;
  186. }

comments powered by Disqus