Program to Convert Number to Words in C#

Program to convert number to words in C#-Learn how to convert a digit into word format with explanation and with Example program.

Program to convert number to words in C#

Converting a number into word format means , based on the given input number we have to spell it into words.

For Example the Given input number is 0, then in word format it has to display as ZERO. if the given input number is 99 then in word format it has to display as NINETY NINE.

NUMBER             NUMBER IN WORDS
 0                     ZERO
 1                     ONE
11                     ELEVEN
99                     NINETY NINE
111                    ONE HUNDRED AND ELEVEN
1110                   ONE THOUSAND  ONE HUNDRED AND TEN

for below Example we can test with upto a 5 digit Number.

Example Program

class Program
{
static void Main(string[] args)
{
//initializing required values in string Array variable naming a,b,c
string[] a = {“ZERO” ,”ONE”, “TWO”, “THREE”, “FOUR”, “FIVE”, “SIX”, “SEVEN”, “EIGHT”, “NINE” };
string[] b = {“TEN”,”ELEVEN”, “TWELVE”, “THIRTEEN”, “FOURTEEN”, “FIFTEEN”, “SIXTEEN”, “SEVENTEEN”, “EIGHTEEN”, “NINTEEN” };
string[] c = { “ZERO”,”TEN”, “TWENTY”, “THIRTY”, “FOURTY”, “FIFTY”, “SIXTY”, “SEVENTY”, “EIGHTY”, “NINTY” ,”HUNDRED”};

int number = 99011, temp;
if (number <= 99999)
{
if (number==0)
{
Console.Write(” ” + a[number]);

}

if (number >= 1000)
{

temp = number / 1000;
if (number % 10000 == 0)
{
temp = temp / 10;
Console.Write(b[temp-1]+ ” THOUSAND “);
return;

}

if (number % 1000 == 0)
{
Console.Write(a[temp] + ” THOUSAND “);
return;
}
if (temp >= 10 && temp<=19)
{
temp = temp %10;
Console.Write(” “+b[temp] + ” “);
temp = -1;
}
if (temp >= 20 && temp<=99)
{
int i = temp / 10;
temp = temp % 10;
Console.Write(” ” + c[i] + ” “);

}
if(temp >0 && temp<=9)
{
Console.Write(” “+a[temp]+” “);
}

Console.Write( ” THOUSAND “);

number = number % 1000;

}
if (number >=100)
{
temp = number / 100;
if (number%100 == 0)
{
Console.Write(a[temp] + ” HUNDRED “);
return;
}

number = number % 100;

Console.Write(a[temp] + ” HUNDRED AND “);

}

if (number >= 10 && number < 20)
{

temp = number % 10;

Console.Write(” ” + b[temp]);

}

if (number > 19 && number <= 100)
{

temp = number / 10;

if(number==100)
{
Console.Write(” ” + c[temp – 1] + ” “);
return;
}

number = number % 10;

Console.Write(” ” + c[temp] + ” “);

}
if (number < 10)
{
if (number!=0)
Console.Write(” “+a[number]);

}

}
else
{
Console.WriteLine(“Enter a small number”);
}

}
}

Output

NINTY NINE THOUSAND ELEVEN

Example Program-Using Switch case

For below Example you can test up to a 10 digit Number.

class Program
{
static void Main(string[] args)
{
//initializing size for 2 dimensional Chara Array
string[] a = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
string[] b = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINTEEN" };
string[] c = { "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINTY" };
string[] d = { "zero", "TEN", "HUNDRED", "THOUSAND", "THOUSAND", "LAKHS", "LAKHS", "CRORES", "CRORES", "HUNDRED AND" };
//99911191191
// NINE HUNDRED AND NINTY ONE CRORES ELEVEN LAKHS NINTY ONE THOUSAND ONE HUNDRED AND NINTY ONE

//long number = 9999999999, temp = 0, strlen, value = 0;
long number, temp = 0, strlen, value = 0;
Console.WriteLine("please enter integer ");
number = Convert.ToInt64(Console.ReadLine());

Boolean flag = false;
if (number <= 9999999999)
{
while (number >= 0)
{
strlen = number.ToString().Length;
if (number > 0)
{

value = (long)Math.Pow(10, strlen - 1);
temp = number / value;


}

switch (strlen)
{
case 1: Console.Write(" " + a[temp - 1] + " ");

break;
case 2:
if (number >= 10 && number < 20)
{
number = number % value;
if (number == 0)
{
Console.Write(" " + b[number] + " ");
}
else
{
Console.Write(" " + b[number] + " ");
}

flag = true;

}
else
{
Console.Write(" " + c[temp - 1] + " ");
}

break;
case 3:
Console.Write(a[temp - 1] + " " + d[strlen - 1]);
break;
case 4:
case 6:
case 8:
case 10:
Console.Write(" " + a[temp - 1] + " " + d[strlen - 1] + " ");
break;
case 5:
case 7:
case 9:
case 11:
value = (long)Math.Pow(10, strlen - 2);
temp = number / value;
if (temp >= 10 && temp < 20)
{
temp = temp % 10;
if (temp == 0)
{
Console.Write(" " + c[temp] + " " + d[strlen - 1] + " ");
}
else
{
Console.Write(" " + b[temp] + " " + d[strlen - 1] + " ");

}

}
else
{
long i = temp % 10;
temp = temp / 10;
Console.Write(" " + c[temp - 1] + " ");
if (i > 0)
{
Console.Write(" " + a[i - 1] + " ");

}
Console.Write(" " + d[strlen - 1] + " ");
}
break;


}
if (number == value)
{
Console.Write(" ");
break;
}
else
{
number = number % value;


}
if (number == 0 || flag == true)
{
break;
}

flag = false;

}
}
else
{
Console.WriteLine("enter numner less than 9999999999");

}

}
}

INPUT
please enter integer
945910023
OUTPUT
NINTY FOUR CRORES FIFTY NINE LAKHS TEN THOUSAND TWENTY THREE

Related Posts