Advertisement
aslv

Number as Words

Mar 23rd, 2014
2,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System;
  2.  
  3. namespace NumberAsWords
  4. {
  5.     class NumberWord
  6.     {
  7.         static string[] one = new string[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
  8.         static string[] teens = new string[] { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeeen", "eighteen", "nineteen" };
  9.         static string[] ten = new string[] { null, null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
  10.  
  11.         static void Main()
  12.         {
  13.             int a;
  14.             if (int.TryParse(Console.ReadLine(), out a))
  15.             {
  16.                 if (a > 999 || a < 0)
  17.                 {
  18.                     Console.Error.WriteLine("the number is either too big or negative");
  19.                     Environment.Exit(1);
  20.                 }
  21.             }
  22.             else
  23.             {
  24.                 Console.Error.WriteLine("invalid number");
  25.                 Environment.Exit(1);
  26.             }
  27.  
  28.             int hundreds = a / 100;
  29.             int tens = (a / 10) % 10;
  30.             int ones = a % 10;
  31.             string wordRepresentation = "";
  32.  
  33.             if (hundreds == 0)
  34.             {
  35.                 if (tens == 0)
  36.                 {
  37.                     wordRepresentation = one[ones];
  38.                 }
  39.                 else
  40.                 {
  41.                     if (tens == 1)
  42.                     {
  43.                         wordRepresentation = teens[ones];
  44.                     }
  45.                     else
  46.                     {
  47.                         wordRepresentation = ten[tens];
  48.                         if (ones > 0)
  49.                         {
  50.                             wordRepresentation += " " + one[ones];
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.             else
  56.             {
  57.                 wordRepresentation = one[hundreds] + " hundred";
  58.                 if (tens == 0)
  59.                 {
  60.                     if (ones != 0)
  61.                     {
  62.                         wordRepresentation += " and " + one[ones];
  63.                     }
  64.                 }
  65.                 else
  66.                 {
  67.                     if (tens == 1)
  68.                     {
  69.                         wordRepresentation += " and " + teens[ones];
  70.                     }
  71.                     else
  72.                     {
  73.                         wordRepresentation += " and " + ten[tens];
  74.                         if (ones > 0)
  75.                         {
  76.                             wordRepresentation += " " + one[ones];
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             Console.WriteLine("{0} = {1}", a, wordRepresentation);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement