beBoss

Number as words

Mar 21st, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2.  
  3. //Problem 11.   * Number as Words
  4.  
  5. //Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation. Examples:
  6. //numbers   number as words
  7. //0         Zero
  8. //9         Nine
  9. //10        Ten
  10. //12        Twelve
  11. //19        Nineteen
  12. //25        Twenty five
  13. //98        Ninety eight
  14. //273       Two hundred and seventy three
  15. //400       Four hundred
  16. //501       Five hundred and one
  17. //617       Six hundred and seventeen
  18. //711       Seven hundred and eleven
  19. //999       Nine hundred and ninety nine
  20.  
  21. class NumberAsWords
  22. {
  23.     static string UppercaseFirst(string s)
  24.     {
  25.         char[] a = s.ToCharArray();
  26.         a[0] = char.ToUpper(a[0]);
  27.         return new string(a);
  28.     }
  29.  
  30.     static void Main()
  31.     {
  32.         string[] words =
  33.         {
  34.             "zero", "one", "two", "three", "four", "five", "six",
  35.             "seven", "eight", "nine", "ten", "eleven", "twelve",
  36.             "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
  37.             "eighteen", "nineteen", "twenty", "thirty", "forty",
  38.             "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
  39.         };
  40.  
  41.         Console.Write("Enter a number [0…999] : ");
  42.  
  43.         ushort num;
  44.         if (!ushort.TryParse(Console.ReadLine(), out num))
  45.         {
  46.             Console.WriteLine("The input isn't a number!");
  47.         }
  48.         else
  49.         {
  50.             if ((num > 99) && (num < 1000))
  51.             {
  52.                 Console.WriteLine(
  53.                     (double)num / (num / 100) == 100 ?
  54.                     UppercaseFirst(words[num / 100]) + " " + words[words.Length - 1] :
  55.                     UppercaseFirst(words[num / 100]) + " " + words[words.Length - 1] + " " +
  56.                     (num % 100 / 10 == 0 ? "and" + " " + words[num % 10] : (num % 100 < 21 ? "and " +
  57.             words[(num % 100)] : "and " + words[18 + (num % 100 / 10)] + " " + (num % 10 != 0 ? words[num % 10] : " "))));
  58.             }
  59.             else if (num < 100)
  60.             {
  61.                 Console.WriteLine(num % 100 / 10 == 0 ?
  62.                     UppercaseFirst(words[num % 10]) : (num < 21 ? UppercaseFirst(words[num]) :
  63.                     UppercaseFirst(words[18 + (num % 100 / 10)]) + " " +
  64.                     (num % 10 != 0 ? words[num % 10] : " ")));
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine("The number is bigger then 1000!");
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment