Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to convert digit into words...

Posted By: Easy Tutor     Category: C Programming     Views: 29080

Write a program to convert digit into words.
e.g :
i/p:
123

o/p :
one hundered twenty three

Code for Program to convert digit into words... in C Programming

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main(){
    char init[27][12] = {" one "," two "," three ",
                " four "," five "," six ",
                " seven "," eight "," nine ",
                " ten "," eleven "," twelve ",
                " thirteen "," fourteen "," fifteen ",
                " sixteen "," seventeen "," eighteen ",
                " nineteen "," twenty "," thirty ",
                " fourty "," fifty "," sixty ",
                " seventy "," eight "," ninty "};
    char sthou[20]="",shund[20]="",sval1[20]="",sval2[20]="",result[100]="";
    int thou=0,hund=0,ten=0,temp=0,val1,val2,num,rem,c=0;

clrscr();
//USING COBOL LOGIC
     printf("*****AMOUNT IN WORDS*****\n\n");
     printf("Enter any value (upto 4 digits) : ");
     scanf("%d",&num);
     while(num>0){
         rem = num%10;
         c++;
         if(c<=2)
        temp = temp * 10  +rem;
         elseif(c==3)
        hund = rem;
         elseif(c==4)
        thou = rem;
         num = num/10;
     }
     while(temp>0){  //as ten contains two digit so reverse it
        rem = temp%10;
        ten = ten * 10 + rem;
        temp= temp/10;
     }

     if(thou>0){
       strcpy(sthou,init[thou-1]);
       strcat(sthou," thousand ");
       strcat(result,sthou);
     }

     if(hund>0){
       strcpy(shund,init[hund-1]);
       strcat(shund," hundred ");
       strcat(result,shund);
     }

     if(ten>0){
          if(ten>20){
          val1 = ten/10;
          val2 = ten%10;
          }
          if(val1>0){
           strcpy(sval1,init[val1+(18-1)]);
           strcat(result,sval1);
          }
          if(val2>0){
           strcpy(sval2,init[val2-1]);
           strcat(result,sval2);
          }
     }
     printf("\n\nAmount in word is as under \n");
     printf("%s",result);

getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program to convert digit into words... Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to convert digit into words... is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Ganesh Alluri from United States Comment on: Dec 02
Can you please check the above code for 1011 number or any number with zero in any position.

View All Comments