Tuesday 24 July 2012

Enter a four digit number and display it in words conversion

C Program to enter a four digit number and display it in words
void main()

{

int n,num,d=0,dig[4];


//Array of strings for numbers from one two ten

char *ones[]={" ", "One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};


//Array of strings for numbers from ten to nineteen

char *el[]={"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};


// Array of strings for numbers from twenty , thirty ... upto ninety

char *tens[]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};


//Accept four digit number

printf("\nEnter a four digit number:-\t");

scanf("%d",&num);


//store the number in another variable

n=num;


//Calculate number of digits in the given number

do

{

dig[d]=n%10;

n/=10;

d++;

}while(n>0);


//Display the number in words depending upon the number of digits

if(d==4)

printf(" %s Thousand",ones[dig[3]]);

if(d>=3&&dig[2]!=0)

printf(" %s Hundred",ones[dig[2]]);


if(d>=2)

{

if(dig[1]==0)

printf(" %s\n",ones[dig[0]]);

else if(dig[1]==1)

printf(" %s\n",el[dig[0]]);

else

printf(" %s %s \n",tens[dig[1]],ones[dig[0]]);

}


if(d==1 && num!=0)

printf(" %s %s \n",ones[dig[0]]);

if(num==0)

printf(" Zero\n");

}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: