using System; using System.Reflection; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; using System.Text; using System.Data; namespace ConsoleApplication1 { class NumbersPositionsInWords { public string zeroPosition;//One, Two, Three public string tenthPosition;// Holds Twenty, Thirty, etc. public string elevenSeries;//Special words like 11 = Eleven, 12 = Tweleve, etc. } class Program { static Dictionary numbersToWordsDictionary = new Dictionary(); static string hundredString = "Hundred"; static StringBuilder numberToWordText = new StringBuilder(); static Program() { numbersToWordsDictionary.Add(1, new NumbersPositionsInWords() { zeroPosition = "One", tenthPosition = "Ten", elevenSeries = "Eleven" }); numbersToWordsDictionary.Add(0, new NumbersPositionsInWords() { zeroPosition = "Zero", tenthPosition = null, elevenSeries = "Ten" }); numbersToWordsDictionary.Add(2, new NumbersPositionsInWords() { zeroPosition = "Two", tenthPosition = "Twenty", elevenSeries = "Tweleve" }); numbersToWordsDictionary.Add(3, new NumbersPositionsInWords() { zeroPosition = "Three", tenthPosition = "Thirty", elevenSeries = "Thirteen" }); numbersToWordsDictionary.Add(4, new NumbersPositionsInWords() { zeroPosition = "Four", tenthPosition = "Fourty", elevenSeries = "Fourteen" }); numbersToWordsDictionary.Add(5, new NumbersPositionsInWords() { zeroPosition = "Five", tenthPosition = "Fifty", elevenSeries = "Fifteen" }); numbersToWordsDictionary.Add(6, new NumbersPositionsInWords() { zeroPosition = "Six", tenthPosition = "Sixty", elevenSeries = "Sixteen" }); numbersToWordsDictionary.Add(7, new NumbersPositionsInWords() { zeroPosition = "Seven", tenthPosition = "Seventy", elevenSeries = "Seventeen" }); numbersToWordsDictionary.Add(8, new NumbersPositionsInWords() { zeroPosition = "Eight", tenthPosition = "Eighty", elevenSeries = "Eighteen" }); numbersToWordsDictionary.Add(9, new NumbersPositionsInWords() { zeroPosition = "Nine", tenthPosition = "Ninty", elevenSeries = "Ninteen" }); } public static void Main(string[] args) { Console.WriteLine("Enter a 3digit number"); string inputString = Console.ReadLine(); ValidateInput(inputString); int hundredthPostionDigit = GetDigitFromPosition(inputString, 0); StringBuilder resultString = new StringBuilder(); if (hundredthPostionDigit > 0) resultString.AppendFormat("{0}{1} {2}", numbersToWordsDictionary[hundredthPostionDigit].zeroPosition, string.Empty, hundredString); int tenthPositionDigit = GetDigitFromPosition(inputString, 1); int unitsPositionDigit = GetDigitFromPosition(inputString, 2); ParseTenthUnitPositionDigit(tenthPositionDigit, unitsPositionDigit); if (resultString.Length > 0 && numberToWordText.Length > 0) resultString.AppendFormat(" {0} {1}", "And", numberToWordText); else resultString.Append(numberToWordText); Console.WriteLine(resultString); Console.ReadLine(); } private static void ValidateInput(string str) { int dummy; if (str.Length > 3 || str.Length < 3 || !Int32.TryParse(str, out dummy)) { Console.WriteLine("Enter numbers with only 3 digits. Press Enter key to close application."); Console.ReadLine(); Environment.Exit(0); } } private static void ParseTenthUnitPositionDigit(int tenthPositionDigit, int unitsPositionDigit) { if (tenthPositionDigit > 1) numberToWordText.AppendFormat("{0} {1}", numbersToWordsDictionary[tenthPositionDigit].tenthPosition, unitsPositionDigit != 0 ? numbersToWordsDictionary[unitsPositionDigit].zeroPosition : string.Empty); if (tenthPositionDigit < 1) numberToWordText.AppendFormat("{0}", unitsPositionDigit > 0 ? numbersToWordsDictionary[unitsPositionDigit].zeroPosition : string.Empty); if (tenthPositionDigit == 1) numberToWordText.AppendFormat("{0}", unitsPositionDigit == tenthPositionDigit ? numbersToWordsDictionary[tenthPositionDigit].elevenSeries : numbersToWordsDictionary[unitsPositionDigit].elevenSeries); } private static int GetDigitFromPosition(string str, int position) { return Int32.Parse(str[position].ToString()); } } }