c#


SUBMITTED BY: Guest

DATE: Nov. 23, 2013, 1:23 p.m.

FORMAT: Text only

SIZE: 5.2 kB

HITS: 834

  1. http://cur.lv/4x9e0 (Earn Bitcoins)
  2. /* * Write a program that converts a number in the range [0...999] to a text corresponding to its English pronunciation. Examples:
  3. 0 -> "Zero"
  4. 273 -> "Two hundred seventy three"
  5. 400 -> "Four hundred"
  6. 501 -> "Five hundred and one"
  7. 711 -> "Seven hundred and eleven"
  8. */
  9. using System;
  10. class NumberToText
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.Write("Enter a number between [1..999]: ");
  15. int number = int.Parse(Console.ReadLine());
  16. string textNumber = "";
  17. int digits = number % 10;
  18. int tens = number % 100;
  19. int hundreds = number / 100;
  20. if (hundreds != 0)
  21. {
  22. switch (hundreds)
  23. {
  24. case 1: textNumber += "One hundred "; break;
  25. case 2: textNumber += "Two hundred "; break;
  26. case 3: textNumber += "Three hundred "; break;
  27. case 4: textNumber += "Four hundred "; break;
  28. case 5: textNumber += "Five hundred "; break;
  29. case 6: textNumber += "Six hundred "; break;
  30. case 7: textNumber += "Seven hundred "; break;
  31. case 8: textNumber += "Eight hundred "; break;
  32. case 9: textNumber += "Nine hundred "; break;
  33. }
  34. if (tens / 10 == 0 && digits != 0)
  35. {
  36. textNumber += "and ";
  37. switch (digits)
  38. {
  39. case 0: textNumber += "zero"; break;
  40. case 1: textNumber += "one"; break;
  41. case 2: textNumber += "two"; break;
  42. case 3: textNumber += "three"; break;
  43. case 4: textNumber += "four"; break;
  44. case 5: textNumber += "five"; break;
  45. case 6: textNumber += "six"; break;
  46. case 7: textNumber += "seven"; break;
  47. case 8: textNumber += "eight"; break;
  48. case 9: textNumber += "nine"; break;
  49. }
  50. }
  51. }
  52. if (tens < 20)
  53. {
  54. switch (tens)
  55. {
  56. case 10: textNumber += "ten"; break;
  57. case 11: textNumber += "eleven"; break;
  58. case 12: textNumber += "twelve"; break;
  59. case 13: textNumber += "thirteen"; break;
  60. case 14: textNumber += "fourteen"; break;
  61. case 15: textNumber += "fifteen"; break;
  62. case 16: textNumber += "sixteen"; break;
  63. case 17: textNumber += "seventeen"; break;
  64. case 18: textNumber += "eighteen"; break;
  65. case 19: textNumber += "nineteen"; break;
  66. }
  67. }
  68. else
  69. {
  70. switch (tens / 10)
  71. {
  72. case 2: textNumber += "twenty "; break;
  73. case 3: textNumber += "thirty "; break;
  74. case 4: textNumber += "fourty "; break;
  75. case 5: textNumber += "fifty "; break;
  76. case 6: textNumber += "sixty "; break;
  77. case 7: textNumber += "seventy "; break;
  78. case 8: textNumber += "eigthy "; break;
  79. case 9: textNumber += "ninety "; break;
  80. }
  81. if (digits != 0)
  82. {
  83. switch (digits)
  84. {
  85. case 0: textNumber += "zero"; break;
  86. case 1: textNumber += "one"; break;
  87. case 2: textNumber += "two"; break;
  88. case 3: textNumber += "three"; break;
  89. case 4: textNumber += "four"; break;
  90. case 5: textNumber += "five"; break;
  91. case 6: textNumber += "six"; break;
  92. case 7: textNumber += "seven"; break;
  93. case 8: textNumber += "eight"; break;
  94. case 9: textNumber += "nine"; break;
  95. }
  96. }
  97. }
  98. if (hundreds == 0 && tens/10 == 0)
  99. {
  100. switch (digits)
  101. {
  102. case 0: textNumber += "zero"; break;
  103. case 1: textNumber += "one"; break;
  104. case 2: textNumber += "two"; break;
  105. case 3: textNumber += "three"; break;
  106. case 4: textNumber += "four"; break;
  107. case 5: textNumber += "five"; break;
  108. case 6: textNumber += "six"; break;
  109. case 7: textNumber += "seven"; break;
  110. case 8: textNumber += "eight"; break;
  111. case 9: textNumber += "nine"; break;
  112. }
  113. }
  114. Console.WriteLine("{0} -> {1}", number, textNumber);
  115. }
  116. }

comments powered by Disqus