C program Number to Text 2 conversion

This C program will convert the number to words in tens and units place respectively. This is a part of Mumbai University MCA Colleges C programs MCA Sem 1
Out put as

Input:- 12345
Output:- twelve thousand three hundred fourty five


#include<stdio.h>

void pw(long,char[]);//function
char *unit[]={" "," one"," two"," three"," four"," five"," six"," seven","eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};

void main()
{
long n;
printf("Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");//Will give either 1 or 2 digit answer
pw(((n/100000)%100),"lakh");//Will give either 1 or 2 digit answer
pw(((n/1000)%100),"thousand");//Will give either 1 or 2 digit answer
pw(((n/100)%10),"hundred");//Will always give 1 digit answer
pw((n%100)," ");////Will give always 2 digit answer
}
}


void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],unit[n%10]):printf("%s ",unit[n]);
/*This if condition will execute if n is greater than 0 and used to print crore, lakh, thousand and hundred
This is used so that if say we enter 12000 the answer must be "twelve thousand"
If you omit the if statement and simply write the print statement the output would be like
"twelve thousand hundred" which is not the right one
*/
if(n)
printf("%s ",ch);
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment