c program to print digits in words

Following c program illustrate how to print digits into words. The code will take an input value of from the user and it prints each digit into correspond word format. For example, if “1234” is given as input, the output should be “one two three four”.

#include <stdio.h>
int main()
{
  int n,rn=0,count=0;
  printf("\nEnter a value: ");
  scanf("%d",&n);
  while(n)        //This loop makes input value into reverseing number
  {
    rn=rn*10+n%10;
    ++count;
    n=n/10;
  }
  while(rn)       //Here reverse number will print in reverse order i.e actual number 
  {
    switch(rn%10)
    {
      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;
   }
   rn=rn/10;
   --count;
  }
  while(count>0)
  {
    printf(" Zero");
    --count;
  }
}

[quote]

Output:

Enter a value: 12345                                                                                                                                         
One Two Three Four Five 

[/quote]

Limitation

If the number starts with zero(s) then it will ignore those digits

Example

[quote]

Enter a value: 00123                                                                                                                                         
One Two Three   

[/quote]

Leave a Reply

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