Write C++ program to print number in words


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n, num = 0;
  8.  
  9. //Reading a number from user
  10. cout<<"Enter any number to print in words: ";
  11. cin>>n;
  12.  
  13. while(n != 0)
  14. {
  15. num = (num * 10) + (n % 10);
  16. n /= 10;
  17. }
  18. //print corresponding digit in words till num becomes 0
  19. while(num != 0)
  20. {
  21. switch(num % 10)
  22. {
  23. case 0: cout<<"zero ";
  24. break;
  25. case 1: cout<<"one ";
  26. break;
  27. case 2: cout<<"two ";
  28. break;
  29. case 3: cout<<"three ";
  30. break;
  31. case 4: cout<<"four ";
  32. break;
  33. case 5: cout<<"five ";
  34. break;
  35. case 6: cout<<"six ";
  36. break;
  37. case 7: cout<<"seven ";
  38. break;
  39. case 8: cout<<"eight ";
  40. break;
  41. case 9: cout<<"nine ";
  42. break;
  43. }
  44.  
  45. num = num / 10;
  46. }
  47.  
  48. return 0;
  49. }
  50.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts