C Program: Converts number in digits to equivalent word

CWC
4 Min Read

This is a simple program  to convert number into words, for example if user enters “2451” then output will be “two thousand four hundred and fifty one”.

We start our code by including the header files “stdio.h”, ”conio.h” and “stdlib.h”. Now we initialize values 1-9 in double array o[][], 10-19 in array t1[][] and values like 20,30,40 till 90 in array t2[][]

char o[9][10]={"one","two","three","four","five","six","seven","eight","nine"};
char t1[10][10]={"ten","eleven","twelve","thirtenn","fourteen","fiftenn","sixteen","seventeen","eighteen","ninteen"};
char t2[8][10]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};

Now we ask the user to input the number which is to be converted in words

printf("enter the number");
scanf("%d",&num);

Convert number into words: For one digit number

Now the logic begins, if the number entered is less than 10 it will enter a for loop which will run till the value of “i” goes 0-10 and check if the number entered is equal to “i” if it is then array a[i] will be displayed on the output screen.

if(num<10)
{
for(i=1;i<10;i++)
{
if (i==num)
printf("output of entered numer in words is= %s ",o[i-1]);
}
}

Convert number into words: For two digit number

Now if the number entered is a two-digit number and is between 10-19 then it will enter the second condition, this condition is same as the first one it’s just that before it was checked that if the value of i is equal to value of number entered but now value of j will be checked instead of i as j is equals to i+10.

for(i=0;i<10;i++)
{
j=i+10;
if (j==num)
printf("output of entered number in words is= %s ",t1[i]);
}

Now if, number is between 20- 99
then the output of the program is is displayed in two parts

1)The tens digit is found out by dividing the number and storing in an variable and then simply checking is inside the for loop and if its equal to the value of “i”  then t1[i] is displayed as the first part.

2)Now the number is deducted from the original number so the ones part remains and then it is also checked in a for loop and then displayed as the second part.

Convert number into words: For three digit number

That is if the num is larger than 99 and smaller than 1000 then the same process if applied as in before, the number is divided into 3 parts the first is the hundred and it is displayed as the hundreds place digit is printed as it is in words using array “o[]” just adding the word “hundred” after it. After the hundreds place the left digits are checked in the are two digits or ones as in case if a number Is “201” then there is no tens digit.

Convert number into words: For four digit number

4 digit number is also displayed in same manner as 3 digit manner first finding out the thousands place digit and displaying it as it is in words and just adding a thousand after it. After this the same logic is applied for hundred, tens and ones place as applied earlier.

My program only runs for a 4 digit number that is till “9999” and I am assuming that user will enter a value more than 0, but you can extend it for more digit number too by using same logic.

C Program: Converts number in digits to equivalent word Output

Download Source Code – C Program: Converts number in digits to equivalent word

[sociallocker]Download Source Code – C Program: Converts number in digits to equivalent word
Password:codewithc.com[/sociallocker]

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version