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;
- namespace _16.Number_0_to_100_to_Text
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Въведи число от 0 до 100: ");
- int number = int.Parse(Console.ReadLine());
- if (number < 0 || number > 100)
- {
- Console.WriteLine("Invalid number");
- }
- else
- {
- string numberAsText = String.Empty; //Еднакво с = "";
- if (number == 0)
- {
- numberAsText = "zero";
- }
- if (number >= 10 && number < 20)
- {
- switch (number)
- {
- case 10: numberAsText = "ten"; break;
- case 11: numberAsText = "eleven"; break;
- case 12: numberAsText = "twelve"; break;
- case 13: numberAsText = "thirteen"; break;
- case 14: numberAsText = "forteen"; break;
- case 15: numberAsText = "fifteen"; break;
- case 16: numberAsText = "sixteen"; break;
- case 17: numberAsText = "seventeen"; break;
- case 18: numberAsText = "eighteen"; break;
- case 19: numberAsText = "nineteen"; break;
- }
- }
- else if ((number > 0 && number < 10) || (number >= 20 && number < 100))
- {
- int firstDigit = number / 10;
- switch (firstDigit)
- {
- case 0: numberAsText = String.Empty; break; //може да се изпиши = ""; break;
- case 2: numberAsText = "twenty"; break;
- case 3: numberAsText = "thirty"; break;
- case 4: numberAsText = "forty"; break;
- case 5: numberAsText = "fifty"; break;
- case 6: numberAsText = "sixty"; break;
- case 7: numberAsText = "seventy"; break;
- case 8: numberAsText = "eighty"; break;
- case 9: numberAsText = "ninety"; break;
- }
- int secondDigit = number % 10;
- switch (secondDigit)
- {
- case 1: numberAsText += " one"; break;
- case 2: numberAsText += " two"; break;
- case 3: numberAsText += " three"; break;
- case 4: numberAsText += " four"; break;
- case 5: numberAsText += " five"; break;
- case 6: numberAsText += " six"; break;
- case 7: numberAsText += " seven"; break;
- case 8: numberAsText += " eight"; break;
- case 9: numberAsText += " nine"; break;
- }
- }
- else if (number == 100)
- {
- numberAsText = "one hundred";
- }
- Console.WriteLine(numberAsText.Trim()); // Trim - е метод и премахва празни интервали пред и след стринга (текста)
- }
- }
- }
- }
- Втори вариант на решение на задачата:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _16._1.Number_0_to_100_to_Text
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Решение с използване на масиви.");
- Console.Write("Въведи цяло число от 0 до 100: ");
- int number = int.Parse(Console.ReadLine());
- if (number < 0 || number > 100)
- {
- Console.WriteLine("Invalid number");
- }
- else
- {
- string nameNumber = "";
- var zeroToNineteen = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
- var twentyToNinety = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
- if (number >= 0 && number < 20)
- {
- nameNumber = zeroToNineteen[number];
- }
- else if (number >= 20 && number < 100)
- {
- int firstDigit = (number / 10);
- nameNumber = twentyToNinety[firstDigit];
- int secondDigit = (number % 10);
- if (secondDigit != 0)
- {
- nameNumber = nameNumber + " " + zeroToNineteen[secondDigit];
- }
- }
- else if (number == 100)
- {
- nameNumber = "one hundred";
- }
- Console.WriteLine(nameNumber);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement