Advertisement
kamandos

HomeWork

Jul 15th, 2022
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.     int n, num = 0, digits;
  7.  
  8.     /* Input number from user */
  9.     printf("Enter any number to print in words: ");
  10.     scanf("%d", &n);
  11.    
  12.     /* Find total digits in n */
  13.     digits = (int) log10(n);
  14.  
  15.     /* Store reverse of n in num */
  16.     while(n != 0)
  17.     {
  18.         num = (num * 10) + (n % 10);
  19.         n /= 10;
  20.     }
  21.    
  22.     /* Find total trailing zeros */
  23.     digits =  digits - ((int) log10(num));  
  24.  
  25.     /*
  26.      * Extract last digit of number and print corresponding number in words
  27.      * till num becomes 0
  28.      */
  29.     while(num != 0)
  30.     {
  31.         switch(num % 10)
  32.         {
  33.             case 0:
  34.                 printf("Zero ");
  35.                 break;
  36.             case 1:
  37.                 printf("One ");
  38.                 break;
  39.             case 2:
  40.                 printf("Two ");
  41.                 break;
  42.             case 3:
  43.                 printf("Three ");
  44.                 break;
  45.             case 4:
  46.                 printf("Four ");
  47.                 break;
  48.             case 5:
  49.                 printf("Five ");
  50.                 break;
  51.             case 6:
  52.                 printf("Six ");
  53.                 break;
  54.             case 7:
  55.                 printf("Seven ");
  56.                 break;
  57.             case 8:
  58.                 printf("Eight ");
  59.                 break;
  60.             case 9:
  61.                 printf("Nine ");
  62.                 break;
  63.         }
  64.        
  65.         num /= 10;
  66.     }
  67.    
  68.     /* Print all trailing zeros */
  69.     while(digits)
  70.     {
  71.         printf("Zero ");
  72.         digits--;
  73.     }
  74.    
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement