• Source
    1. #include <stdio.h>
    2. #include <conio.h>
    3. #include <string.h>
    4. #include <stdlib.h>
    5.  
    6. #define SIZE 100
    7. char *tens(int n);
    8. char *hundred(int n);
    9.  
    10. int main(){
    11. char *p,*ptr,*temp;
    12. long n;
    13. int a,flag=0,c=0;
    14. char t[][10]={"Thousand","Million","Billion","Trillion"};
    15.  
    16. p=(char *) malloc(SIZE * sizeof(char));
    17. temp=(char *) malloc(SIZE * sizeof(char));
    18. printf("\nEnter a number : ");
    19. scanf(" %ld",&n);
    20. if(n<1000){
    21. ptr=hundred(n);
    22. strcpy(p,ptr);
    23. free(ptr);
    24. } else {
    25. a=n%1000;
    26. ptr=hundred(a);
    27. strcpy(p,ptr);
    28. free(ptr);
    29. n=n/1000;
    30. while (n>0){
    31. a=(n>999)? n%1000 : n;
    32. ptr=hundred(a);
    33. strcpy(temp,ptr);
    34. strcat(temp," ");
    35. strcat(temp,t[c++]);
    36. strcat(temp," ");
    37. strcat(temp,p);
    38. free(p);
    39. p=(char *) malloc(SIZE * sizeof(char));
    40. strcpy(p,temp);
    41. free(temp);
    42. temp=(char *) malloc(SIZE * sizeof(char));
    43. free(ptr);
    44. n=n/1000;
    45. }
    46. }
    47. printf("\n %s",p);
    48. free(p);
    49. getch();
    50. }
    51.  
    52. char *tens(int n){
    53. int a;
    54. char *p,temp[20];
    55. char t1[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
    56. "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    57. char t2[][10]={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    58.  
    59. if(n<20){
    60. strcpy(temp,t1[n]);
    61. } else {
    62. a=n/10-2;
    63. strcpy(temp,t2[a]);
    64. if((a=n%10)!=0) {
    65. strcat(temp," ");
    66. strcat(temp,t1[a]);
    67. }
    68. }
    69.  
    70. p=(char *) malloc((strlen(temp)+1)*sizeof(char));
    71. strcpy(p,temp);
    72. return(p);
    73. }
    74.  
    75. char *hundred(int n){
    76. int a;
    77. char temp[40],*p;
    78. char t[][10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    79.  
    80. if(n>99 && n<1000){
    81. a=n/100;
    82. strcpy(temp,t[a]);
    83. strcat(temp," Hundred ");
    84. a=n%100;
    85.  
    86. p=tens(a);
    87. strcat(temp,p);
    88. free(p);
    89. } else {
    90. p=tens(n);
    91. strcpy(temp,p);
    92. free(p);
    93. }
    94. p=(char *) malloc((strlen(temp)+1)*sizeof(char));
    95. strcpy(p,temp);
    96. return(p);
    97. }
    98.