Number to words c# console application from 0 to 99
In this article we will show you how you can create a console application on C# to turn given numbers into words.
1. Declaring a variable which will be used to give a number to turn it into words
2. Declaring and initializing the string arrays with numbers into words, as shown below
string[] ones = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; // In the array called ones, we stored all numbers from 0-9 into words
string[] twos = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; // In the array called twos, we stored all numbers from 10-19 into words
string[] tens = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}; // In the array called tens, we stored all numbers from 20-90 into words
3. Checking the number
if (number < 10) // This checks if the given number is smaller than 10 (is part of "ones" array)
{
Console.Write("Number {0} into words is {1}", number, ones[number]); // This line of code will get the value of the "ones" on index "number" (if number is 7, it will get the ones[7] word)
}
else if (number > 9 && number < 20) // This checks if the given number is greater than 9 or smaller than 19 (is part of "twos" array)
{
int nr = number - 10; // This subtracts the given number by 10
Console.WriteLine("Number {0} into words is {1}", number, twos[nr]); // This line of code will get the value of the "twos" on index "nr" (if number is 17, it will get the twos[7] word)
}
else if (number > 19 && number <100) // This checks if the given number is greater than 19 or smaller than 100 (is part of "twos" array)
if(number % 10 != 0 ) // This checks if the given number gives us a remainder when divided by 10
{
int remainder = number % 10; // This gives us the remainder of the given number when divided (25, it gives the number 5)
int tenss = number / 10; // This gives us the number to get the tens[tenss] word (if nuber is 25, it gives tens[2] word)
Console.WriteLine("Number {0} into words is {1} {2}", number, tens[tenss - 2], ones[remainder]); // // This line of code will get the value of the "tens" on index "tenss - 2" and "ones" on index "remainder" (number 25 will be "twenty five")
}
else
{
int nr1 = number / 10; // if number has no remainder when divided by 10, it saves that value into nr1 variable
Console.WriteLine("Number {0} into words is {1}", number, tens[nr1 - 2]); // This line of code will get the value of the "tens" on index "nr1 - 2" and "ones"
}
}
Console.ReadLine();
4. Complete code of console application
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.Write("Write the number you want to turn into words: ");
int number = Convert.ToInt32(Console.ReadLine());
string[] ones = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string[] twos = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string[] tens = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if (number < 10)
{
Console.Write("Number {0} into words is {1}", number, ones[number]);
}
else if (number > 9 && number < 20)
{
int nr = number - 10;
Console.WriteLine("Number {0} into words is {1}", number, twos[nr]);
}
else if (number > 19 && number <100)
{
if(number % 10 != 0 )
{
int remainder = number % 10;
int tenss = number / 10;
Console.WriteLine("Number {0} into words is {1} {2}", number, tens[tenss - 2], ones[remainder]);
}
else
{
int nr1 = number / 10;
Console.WriteLine("Number {0} into words is {1}", number, tens[nr1 - 2]);
}
}
Console.ReadLine();
}
}