Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation.
- using System;
- class NumberAsWords
- {
- static void Main()
- {
- int number = 0;
- while (true)
- {
- Console.WriteLine("Enter value between 0 and 999!");
- string userInput = Console.ReadLine();
- number = 0;
- if (int.TryParse(userInput, out number) && number >= 0 && number <= 999)
- {
- break;
- }
- }
- string and = "and";
- string space = " ";
- string oneHundred = "Hundred";
- string zero = "Zero";
- int firstDigit = 0;
- int tenToNineteen = 0;
- int secondDigit = 0;
- int thirdDigit = 0;
- if (number == 0 || number == 100)
- {
- if (number == 0)
- {
- Console.WriteLine(zero);
- return;
- }
- else
- {
- Console.WriteLine(oneHundred);
- return;
- }
- }
- else if (number >= 1 && number <= 9)
- {
- Units(number);
- Console.WriteLine();
- return;
- }
- else if (number >=10 && number <= 19)
- {
- TenToNineteen(number);
- Console.WriteLine();
- return;
- }
- else if (number >= 20 && number <= 99)
- {
- if (number % 10 == 0)
- {
- number /= 10;
- Tens(number);
- Console.WriteLine();
- return;
- }
- else
- {
- firstDigit = number % 10;
- secondDigit = number / 10;
- Tens(secondDigit);
- Console.Write(space);
- Units(firstDigit);
- Console.WriteLine();
- return;
- }
- }
- else if (number > 100)
- {
- if (number % 100 == 0)
- {
- number /= 100;
- Units(number);
- Console.WriteLine(space + oneHundred);
- return;
- }
- else if (number % 10 == 0)
- {
- thirdDigit = number / 100;
- secondDigit = (number / 10) % 10;
- Units(thirdDigit);
- Console.Write(" {0} {1} ", oneHundred, and);
- Tens(secondDigit);
- Console.WriteLine();
- return;
- }
- else if (number % 100 >= 11 && number % 100 <= 19)
- {
- tenToNineteen = number % 100;
- thirdDigit = number / 100;
- Units(thirdDigit);
- Console.Write(" {0} {1} ", oneHundred, and);
- TenToNineteen(tenToNineteen);
- Console.WriteLine();
- return;
- }
- else if ((number / 10) % 10 == 0)
- {
- firstDigit = number % 10;
- thirdDigit = number / 100;
- Units(thirdDigit);
- Console.Write(" {0} {1} ", oneHundred, and);
- Units(firstDigit);
- Console.WriteLine();
- return;
- }
- else
- {
- firstDigit = number % 10;
- secondDigit = (number / 10) % 10;
- thirdDigit = number / 100;
- Units(thirdDigit);
- Console.Write(" {0} {1} ", oneHundred, and);
- Tens(secondDigit);
- Console.Write(space);
- Units(firstDigit);
- Console.WriteLine();
- return;
- }
- }
- }
- static void Units(int units)
- {
- switch (units)
- {
- //case "0": Console.Write("Zero"); break;
- case 1: Console.Write("One"); break;
- case 2: Console.Write("Two"); break;
- case 3: Console.Write("Three"); break;
- case 4: Console.Write("Four"); break;
- case 5: Console.Write("Five"); break;
- case 6: Console.Write("Six"); break;
- case 7: Console.Write("Seven"); break;
- case 8: Console.Write("Eight"); break;
- case 9: Console.Write("Nine"); break;
- }
- }
- static void TenToNineteen(int tenToNineteen)
- {
- switch (tenToNineteen)
- {
- case 10: Console.Write("Ten"); break;
- case 11: Console.Write("Еleven"); break;
- case 12: Console.Write("Тwelve"); break;
- case 13: Console.Write("Thirteen"); break;
- case 14: Console.Write("Fourteen"); break;
- case 15: Console.Write("Fifteen"); break;
- case 16: Console.Write("Sixteen"); break;
- case 17: Console.Write("Seventeen"); break;
- case 18: Console.Write("Eighteen"); break;
- case 19: Console.Write("Nineteen"); break;
- }
- }
- static void Tens(int tens)
- {
- switch (tens)
- {
- case 1: Console.Write("Ten"); break;
- case 2: Console.Write("Twenty"); break;
- case 3: Console.Write("Thirty"); break;
- case 4: Console.Write("Forty"); break;
- case 5: Console.Write("Fifty"); break;
- case 6: Console.Write("Sixty"); break;
- case 7: Console.Write("Seventy"); break;
- case 8: Console.Write("Eighty"); break;
- case 9: Console.Write("Ninety"); break;
- }
- }
- }
- // Examples:
- //numbers number as words
- //0 Zero
- //9 Nine
- //10 Ten
- //12 Twelve
- //19 Nineteen
- //25 Twenty five
- //98 Ninety eight
- //273 Two hundred and seventy three
- //400 Four hundred
- //501 Five hundred and one
- //617 Six hundred and seventeen
- //711 Seven hundred and eleven
- //999 Nine hundred and ninety nine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement