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 a text corresponding to its English pronunciation. Examples:
- 0 "Zero"
- 273 "Two hundred seventy three"
- 400 "Four hundred"
- 501 "Five hundred and one"
- 711 "Seven hundred and eleven"
- */
- using System;
- class NumbersNames
- {
- static void Main()
- {
- string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
- string[] special = { "ten", "eleven", "twelve", "thirdtheen", "fourthen", "fifteen", "sixteen", "seventheen", "eightheen", "ninetheen" };
- string[] dec = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
- int num = 0;
- Console.WriteLine("Please write down number between 0 and 999");
- bool validity = int.TryParse(Console.ReadLine(), out num);
- if (validity && num >= 0 && num < 1000)
- {
- if (num >= 100)
- {
- int c = (num % 10);
- int b = (num / 10) % 10;
- int a = (num / 100) % 10;
- if (((b * 10 + c) < 20) && ((b*10 + c) >= 10))
- {
- Console.WriteLine("{0} hundred and {1}",digits[a],special[c]);
- }
- if (b == 0 && c > 0)
- {
- Console.WriteLine("{0} hundred and {1}", digits[a], digits[c]);
- }
- if ((b * 10 + c) >= 20)
- {
- Console.WriteLine("{0} hundred and {1} {2}", digits[a], dec[b], digits[c]);
- }
- if ((b * 10 + c) == 0)
- {
- Console.WriteLine("{0} hundred", digits[a]);
- }
- }
- else
- {
- if (num > 10)
- {
- int c = (num % 10);
- int b = (num / 10) % 10;
- if (((b * 10 + c) < 20) && ((b * 10 + c) >= 10))
- {
- Console.WriteLine("{0}", special[c]);
- }
- if ((b * 10 + c) > 20)
- {
- Console.WriteLine("{0} {1} ", dec[b], digits[c]);
- }
- }
- else
- {
- int c = (num % 10);
- Console.WriteLine("{0}", digits[c]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement