Advertisement
ntodorova

11_NumberToPronunciation

Nov 2nd, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2.  
  3. class NumberToPronunciation
  4. {
  5.     static void Main()
  6.     {
  7.         int digit;
  8.  
  9.         Console.Write("Please enter а number into the range [0..999]: ");
  10.         string str = Console.ReadLine();
  11.  
  12.         if (!int.TryParse(str, out digit))
  13.         {
  14.             Console.WriteLine("Invalid number: {0}", str);
  15.         }
  16.         else
  17.         {
  18.             while (digit < 0 || digit > 999)
  19.             {
  20.                 Console.WriteLine("Please enter correct number!");
  21.                 string strSecond = Console.ReadLine();
  22.  
  23.                 if (!int.TryParse(strSecond, out digit))
  24.                 {
  25.                     Console.WriteLine("Invalid number: {0}", strSecond);
  26.                 }
  27.             }
  28.  
  29.             int hundreds = digit / 100;
  30.             int tens = (digit / 10) % 10;
  31.             int tensBigger = digit % 100; //Tens used to check whether they are more or less than 20
  32.             int units = (tensBigger) % 10;
  33.  
  34.             string[] numberText = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eitheen", "nineteen" };
  35.  
  36.             string[] tensText = { "twenty", "thirty", "fourthy", "fifthy", "sixty", "seventy", "eithy", "ninety" };
  37.  
  38.             string pronunciation = "";
  39.             string andHundreds = "";
  40.  
  41.             // Get text only for numbers between 0 and 19
  42.             if (digit >= 0 && digit <= 19)
  43.             {
  44.                 pronunciation += numberText[digit];
  45.             }
  46.             else
  47.             {
  48.                 if (hundreds > 0)
  49.                 {
  50.                     pronunciation += numberText[hundreds] + " " + "hundred";
  51.                     andHundreds = " and ";
  52.                 }
  53.  
  54.                 if (tens > 0)
  55.                 {
  56.                     if (tensBigger > 20)
  57.                     {
  58.                         // Tens is - 2 because the tensText starts from the text twenty
  59.                         pronunciation += andHundreds + tensText[tens - 2];
  60.                     }
  61.                     else
  62.                     {
  63.                         pronunciation += andHundreds + numberText[tensBigger];
  64.                     }
  65.                 }
  66.                 else
  67.                 {
  68.                     // If tens are 0
  69.                     pronunciation += " and " + numberText[units];
  70.                 }
  71.  
  72.                 if (tensBigger > 20 && units > 0)
  73.                 {
  74.                     pronunciation += " " + numberText[units];
  75.                 }
  76.  
  77.             }
  78.  
  79.             Console.WriteLine(char.ToUpper(pronunciation[0]) + pronunciation.Substring(1) + '.');
  80.  
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement