Advertisement
AnitaN

05.ConditionalStatementsHomework/11.NumberAsWords

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