Sunday, September 20, 2009

PROGRAM TO CONVERT ENTERED NUMBER TO WORDS

#include "stdio.h"
#include "conio.h"

void print(int,int); /* To print digit from 1 to 19 */
void print_place(int); /* To print place */
void print_digit(int); /* To print numbers 20,30,40,50,60,70,80,90 */

int arr[8] = {0}; /* Global Declaration and Initialization */

void main()
{
long int num,temp[8];
int i,j,k,digit,place = 0,rem,check;
clrscr();
printf("Please, Enter Number\n");
printf("The Number Should be Greater than One and Less than One Crore.\n>> ");
scanf("%ld",&num);
printf("\n>> ");
if(num < 10) print(num,1); if(num > 0 && num < 10000000) { i = 0; do { digit = num % 10; place++; /* To find the places of digit */ temp[i++] = digit; /* Copy to temporary array */ num /= 10; }while(num != 0); i--; j = 0; while(i >= 0)
arr[j++] = temp[i--]; /* Copy array temp to array arr reversly */

i = 0;
j = place-1;
k = place-1;
while(i < j) { if(place == 7) /* For Lakh */ { check = 10 * arr[0] + arr[1]; /* If the Sum of first and second digit */ if(check > 9 && check < 20) /* is betwen 9 and 20 */ { rem = check; /* copy check to rem */ print(rem,place); /* and print */ } else { rem = 10 * arr[0]; /* else multiply the first number with 10 */ print_digit(rem); /* print the number */ print(arr[1],1); /* print the second digit */ print_place(place); /* print place */ } i += 2; /* Increase i by 2 */ place -= 2; /* Decrease place by 2 */ } if(place == 5) /* For Thousand */ { check = 10 * arr[k - 4] + arr[k - 3]; /* Sum of third and fourth digit */ if(check > 9 && check < 20) { rem = check; print(rem,5); } else { rem = 10 * arr[k - 4]; print_digit(rem); print(arr[k - 3],1); if(arr[k - 4] != 0 && arr[k - 3] != 0) print_place(place); } i += 2; place -= 2; } if(place < 3) { check = 10 * arr[k - 1] + arr[k]; if(check > 9 && check < 20)
{
rem = check;
print(rem,place);
}

else
{
rem = 10 * arr[k - 1];
print_digit(rem);
print(arr[k],1);
}
i++;

}

else
print(arr[i++],place--);
}
}

else
printf("Out of Range.");

getch();
}

void print(int num,int place)
{
static int pass = 1;
switch (num)
{
case 1:
printf("One ");
break;

case 2:
printf("Two ");
break;

case 3:
printf("Three ");
break;

case 4:
printf("Four ");
break;

case 5:
printf("Five ");
break;

case 6:
printf("Six ");
break;

case 7:
printf("Seven ");
break;

case 8:
printf("Eight ");
break;

case 9:
printf("Nine ");
break;

case 10:
printf("Ten ");
break;

case 11:
printf("Eleven ");
break;

case 12:
printf("Tweleve ");
break;

case 13:
printf("Thirteen ");
break;

case 14:
printf("Fourteen ");
break;

case 15:
printf("Fifteen ");
break;

case 16:
printf("Sixteen ");
break;

case 17:
printf("Seventeen ");
break;

case 18:
printf("Eighteen ");
break;

case 19:
printf("Nineteen ");
break;

}

if(arr[1] == 0 && arr[2] == 0 && arr[3] == 0 && arr[4] == 0 && arr[5] == 0 && arr[6] == 0)
{
if(pass == 1)
print_place(place);
pass++;
}

if(pass == 1)
print_place(place);
}

void print_digit(int num)
{

switch(num)
{
case 20:
printf("Twenty ");
break;

case 30:
printf("Thirty ");
break;

case 40:
printf("Forty ");
break;

case 50:
printf("Fifty ");
break;

case 60:
printf("Sixty ");
break;

case 70:
printf("Seventy ");
break;

case 80:
printf("Eighty ");
break;

case 90:
printf("Ninety ");
break;

}
}

void print_place(int place)
{
switch (place)
{
case 3:
printf("Hundred ");
break;

case 4:
case 5:
printf("Thousand ");
break;

case 6:
case 7:
printf("Lakh ");
break;

}
}

0 comments:


by Ankit Pokhrel.