Write C# program to print number in words


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

techstudy.org

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10. int n, num = 0;
  11.  
  12. // Reading a number from user
  13. Console.WriteLine("Enter any number to print in words: ");
  14. n = Convert.ToInt32(Console.ReadLine());
  15.  
  16. while (n != 0)
  17. {
  18. num = (num * 10) + (n % 10);
  19. n /= 10;
  20. }
  21.  
  22. //print corresponding digit in words till num becomes 0
  23. while (num != 0)
  24. {
  25. switch (num % 10)
  26. {
  27. case 0: Console.Write("zero ");
  28. break;
  29. case 1: Console.Write("one ");
  30. break;
  31. case 2: Console.Write("two ");
  32. break;
  33. case 3: Console.Write("three ");
  34. break;
  35. case 4: Console.Write("four ");
  36. break;
  37. case 5: Console.Write("five ");
  38. break;
  39. case 6: Console.Write("six ");
  40. break;
  41. case 7: Console.Write("seven ");
  42. break;
  43. case 8: Console.Write("eight ");
  44. break;
  45. case 9: Console.Write("nine ");
  46. break;
  47. }
  48. num = num / 10;
  49.  
  50.  
  51.  
  52. }
  53. Console.ReadLine();
  54. }
  55. }

Result

C# language techstudy

     





Related Topics




Popular Posts