Advertisement
Teodor92

NumberNames

Nov 1st, 2012
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. /* * Write a program that converts a number in the range [0...999] to a text
  2.  * 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.  
  10. using System;
  11.  
  12. class NumbersNames
  13. {
  14.  
  15.     static void Main()
  16.     {
  17.         string[] digits = {"zero" ,"one", "two", "three", "four", "five", "six", "seven", "eight","nine" };
  18.         string[] special = {"ten", "eleven", "twelve", "thirdtheen", "fourthen", "fiftheen", "sixtheen", "seventheen", "eightheen", "ninetheen" };
  19.         string[] dec = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  20.         int input = 0;
  21.         Console.WriteLine("Please enter a number in the range [0, 999]");
  22.         bool isValid = int.TryParse(Console.ReadLine(), out input);
  23.         if (isValid && input >= 0 && input < 1000)
  24.         {
  25.             int digit = input % 10;
  26.             int tens = (input / 10) % 10;
  27.             int hundred = (input / 100) % 10;
  28.  
  29.             if (hundred != 0)
  30.             {
  31.                 Console.Write("{0} hundred ", digits[hundred]);
  32.                 if (tens != 0 && tens != 1 && input >= 20)
  33.                 {
  34.                     Console.Write("and {0} ", dec[tens]);
  35.                     if (digit != 0)
  36.                     {
  37.                         Console.Write("{0} ", digits[digit]);
  38.                     }
  39.                 }
  40.                 else if (tens == 1)
  41.                 {
  42.                     Console.Write("and {0}", special[digit]);
  43.                 }
  44.                 else
  45.                 {
  46.                     if (digit != 0)
  47.                     {
  48.                         Console.Write("and {0} ", digits[digit]);
  49.                     }
  50.                 }
  51.             }
  52.             else
  53.             {
  54.                 // hundreds = 0
  55.                 if (tens != 0 && tens != 1 && input >= 20)
  56.                 {
  57.                     Console.Write("{0} ", dec[tens]);
  58.                     Console.Write("{0} ", digits[digit]);
  59.                 }
  60.                 else if (tens == 1)
  61.                 {
  62.                     Console.Write("{0}", special[digit]);
  63.                 }
  64.                 else
  65.                 {
  66.                     Console.Write("{0} ", digits[digit]);
  67.                 }
  68.  
  69.             }
  70.             Console.WriteLine();
  71.         }
  72.         else
  73.         {
  74.             Console.WriteLine("Out of range, try again!");
  75.             Main();
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement