fork download
  1. #include<iostream>
  2. using namespace std;
  3. void expand(int);
  4. int main()
  5. {
  6. int num;
  7. cout<<"Enter a number : ";
  8. cin>>num;
  9. expand(num);
  10. }
  11. void expand(int value)
  12. {
  13. const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
  14. "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
  15. "eighteen","nineteen"};
  16. const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
  17. "eighty","ninety"};
  18.  
  19. if(value<0)
  20. {
  21. cout<<"minus ";
  22. expand(-value);
  23. }
  24. else if(value>=1000)
  25. {
  26. expand(value/1000);
  27. cout<<" thousand";
  28. if(value % 1000)
  29. {
  30. if(value % 1000 < 100)
  31. {
  32. cout << " and";
  33. }
  34. cout << " " ;
  35. expand(value % 1000);
  36. }
  37. }
  38. else if(value >= 100)
  39. {
  40. expand(value / 100);
  41. cout<<" hundred";
  42. if(value % 100)
  43. {
  44. cout << " and ";
  45. expand (value % 100);
  46. }
  47. }
  48. else if(value >= 20)
  49. {
  50. cout << tens[value / 10];
  51. if(value % 10)
  52. {
  53. cout << " ";
  54. expand(value % 10);
  55. }
  56. }
  57. else
  58. {
  59. cout<<ones[value];
  60. }
  61. return;
  62. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Enter a number : zero