C program to Print Number in Words

Write a C program to print number in words. Write a C program to convert number to characters.

C Program to Print Number in Words

/* Aim: Write a program,which accepts a number n and displays each digit in words.
Example:6278 Output: Six Two Seven Eight */

#include<stdio.h&>
int main()
{ 
	int d,num,rev=0,r;
 
	printf("\n Enter any number:");
	scanf("%d",&num);
	printf("\n ");
  
	do
	{ 
	d=num%10;
	num=num/10;
	rev=rev*10+d;
	}while(num>0);
 
	do
	{ 
	r=rev%10;
	rev=rev/10;
 
	switch(r)
	{
	case 0:
	printf("Zero ");
	break;

	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;
	}}while(rev>0);

	return 0;
}

Output:

 Enter any number:1234

 One Two Three Four

Explanation:

Accept a number from user. Reverse the number. In our case the number accepted was 1234 , its reverse is 4321. Extract the last digit and by using switch case print corresponding word i.e. 'one'. Similarly by using do while loop extract remaining digits and print the corresponding words.