Advertisement
dimov

Untitled

Dec 30th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. /*
  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.  
  10. using System;
  11.  
  12.  
  13. class NumbersNames
  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", "fifteen", "sixteen", "seventheen", "eightheen", "ninetheen" };
  19.         string[] dec = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
  20.         int num = 0;
  21.         Console.WriteLine("Please write down number between 0 and 999");
  22.         bool validity = int.TryParse(Console.ReadLine(), out num);
  23.         if (validity && num >= 0 && num < 1000)
  24.         {
  25.             if (num >= 100)
  26.             {
  27.                 int c = (num % 10);
  28.                 int b = (num / 10) % 10;
  29.                 int a = (num / 100) % 10;
  30.                 if (((b * 10 + c) < 20) && ((b*10 + c) >= 10))
  31.                 {
  32.                     Console.WriteLine("{0} hundred and {1}",digits[a],special[c]);
  33.                 }
  34.                 if (b == 0 && c > 0)
  35.                 {
  36.                     Console.WriteLine("{0} hundred and {1}", digits[a], digits[c]);
  37.                 }
  38.                 if ((b * 10 + c) >= 20)
  39.                 {
  40.                     Console.WriteLine("{0} hundred and {1} {2}", digits[a], dec[b], digits[c]);
  41.                 }
  42.                 if ((b * 10 + c) == 0)
  43.                 {
  44.                     Console.WriteLine("{0} hundred", digits[a]);
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 if (num > 10)
  50.                 {
  51.                     int c = (num % 10);
  52.                     int b = (num / 10) % 10;
  53.                     if (((b * 10 + c) < 20) && ((b * 10 + c) >= 10))
  54.                     {
  55.                         Console.WriteLine("{0}", special[c]);
  56.                     }
  57.                     if ((b * 10 + c) > 20)
  58.                     {
  59.                         Console.WriteLine("{0} {1} ", dec[b], digits[c]);
  60.                     }
  61.                 }
  62.                 else
  63.                 {
  64.                     int c = (num % 10);
  65.                     Console.WriteLine("{0}", digits[c]);                    
  66.                 }                
  67.             }            
  68.         }
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement