Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- // Write a program that converts a number in the range [0…999] to words,
- // corresponding to the English pronunciation.
- class Pr11NumberWords
- {
- static void Main(string[] args)
- {
- Console.Write("Enter how many tests you would like to run: ");
- int tests = int.Parse(Console.ReadLine());
- for (int t = 0; t < tests; t++)
- {
- // Res
- string[] ones = { "zero", "one", "two", "three", "four", "five","six", "seven",
- "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
- "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
- string[] tens = { "twenty", "thirty", "forty", "fifty", "sixty",
- "seventy", "eighty", "ninety" };
- // Input
- string input = Console.ReadLine();
- StringBuilder result = new StringBuilder();
- short number = short.Parse(input);
- short digit1s = (short) (number % 10);
- short digitTeens = (short) (number % 100);
- short digit10s = (short) (digitTeens / 10);
- short digit100s = (short) (number / 100);
- // Zero
- if (number == 0)
- {
- result.Append(ones[0]);
- }
- // Hundreds
- if (digit100s != 0)
- {
- result.Append(ones[digit100s]);
- }
- // Post hundreds
- if (digit100s != 0)
- {
- result.Append(" hundred");
- if (digitTeens != 0)
- {
- result.Append(" and ");
- }
- }
- // Teens
- if (digitTeens > 9 && digitTeens < 20)
- {
- result.Append(ones[digitTeens]);
- }
- // Non-teens
- else
- {
- // Tens
- if (digit10s > 1)
- {
- result.Append(tens[digit10s - 2]);
- }
- // Post tens
- if (digit10s != 0 && digit1s != 0)
- {
- result.Append(" ");
- }
- // Ones
- if (digit1s != 0)
- {
- result.Append(ones[digit1s]);
- }
- }
- // Capitalize first letter
- result[0] = (char) (result[0] - 32);
- string output = result.ToString();
- // Output
- Console.WriteLine(output);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement