Advertisement
Guest User

Untitled

a guest
Jul 26th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2.  
  3. namespace NumberAsWords
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Enter number between 0 and 999:");
  10.             int number = int.Parse(Console.ReadLine());
  11.             string [] toNineteen = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
  12.                 "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
  13.             string [] tensNumbers = {"zero","ten","twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
  14.             string word = "";
  15.  
  16.             if (number >= 0 && number <= 999)
  17.             {
  18.                 if (number / 100 > 0)
  19.                 {
  20.                     word = toNineteen[number / 100] + " " + "hundred";
  21.                     number = number % 100;
  22.                 }
  23.                
  24.                 if (word != "")
  25.                 {
  26.                     word = word + " " + "and ";
  27.                 }
  28.                 if (number < 20)
  29.                 {
  30.                     word = word + toNineteen[number];
  31.                 }
  32.                 else if (number / 10 > 1)
  33.                 {
  34.                     word = word + tensNumbers[number / 10];
  35.                     if (number % 10 > 0)
  36.                     {
  37.                         word = word + " " + toNineteen[number % 10];
  38.                     }
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine("Entered number is negative or/and");
  44.                 Console.WriteLine("is greater than 999");
  45.             }
  46.             Console.WriteLine(word);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement