C# Program to Convert Number into Words

In this C# program, we will take the input number from the user and change it to Words. E.g. 1024 will be converted to One Thousand Twenty Four only.

C# Program to Convert Number into Words Code:

 private static void Main(string[] args)
        {
            Console.Write("Enter a Number: ");
            int number;
            number = int.Parse(Console.ReadLine());
            string word = ConvertNumberToWords(number);

            Console.WriteLine("Number {0} in words is:  {1} only.", number, word.Replace("  ", " "));
            Console.ReadLine();
        }

        private static String[] ones = { "Zero", "One", "Two", "Three",
    "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
    "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
    "Seventeen", "Eighteen", "Nineteen" };
        private static String[] tens = { "", "", "Twenty", "Thirty", "Forty",
    "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

        public static String ConvertNumberToWords(int i)
        {
            if (i < 20)
            {
                return ones[i];
            }
            if (i < 100)
            {
                return tens[i / 10] + ((i % 10 > 0) ? " " + ConvertNumberToWords(i % 10) : "");
            }
            if (i < 1000)
            {
                return ones[i / 100] + " Hundred"
                        + ((i % 100 > 0) ? " And " + ConvertNumberToWords(i % 100) : "");
            }
            if (i < 100000)
            {
                return ConvertNumberToWords(i / 1000) + " Thousand "
                        + ((i % 1000 > 0) ? " " + ConvertNumberToWords(i % 1000) : "");
            }
            if (i < 10000000)
            {
                return ConvertNumberToWords(i / 100000) + " Lakh "
                        + ((i % 100000 > 0) ? " " + ConvertNumberToWords(i % 100000) : "");
            }
            if (i < 1000000000)
            {
                return ConvertNumberToWords(i / 10000000) + " Crore "
                        + ((i % 10000000 > 0) ? " " + ConvertNumberToWords(i % 10000000) : "");
            }
            return ConvertNumberToWords(i / 1000000000) + " Arab "
                    + ((i % 1000000000 > 0) ? " " + ConvertNumberToWords(i % 1000000000) : "");
        }

C# Program to Convert Number into Words Output:

CSharp-program-to-Convert-Number-into-Words

 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice