bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_1 / letusc / luc077.c

luc077.c (1526B)


      1 /* Write a program that receives an integer (less than or equal to nine digits in length) and prints out the number in words.
      2 */
      3 
      4 /* Let Us C, Chap- 16 (Handling Multiple Strings), Qn No.: A(f) */
      5 
      6 /* This file is auto-generated by a bot. */
      7 /* This code is not compiled; it is for reference only. */
      8 
      9 
     10 #include <stdio.h>
     11 #include <string.h>
     12 #include <stdlib.h>
     13 #include <ctype.h>
     14 
     15 void convert(long, char *);
     16 
     17 char *one[] = {
     18     "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", 
     19     "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", 
     20     "Seventeen ", "Eighteen ", "Nineteen "
     21 };
     22 
     23 char *ten[] = {
     24     "", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "
     25 };
     26 
     27 int main()
     28 {
     29     long num;
     30 
     31     printf("Enter a number (max 9 digits): ");
     32     scanf("%ld", &num);
     33 
     34     if (num <= 0)
     35     {
     36         printf("Zero\n");
     37     }
     38     else
     39     {
     40         printf("In words: ");
     41         // Indian System: Crore, Lakh, Thousand, Hundred
     42         convert((num / 10000000), "Crore ");
     43         convert(((num / 100000) % 100), "Lakh ");
     44         convert(((num / 1000) % 100), "Thousand ");
     45         convert(((num / 100) % 10), "Hundred ");
     46         convert((num % 100), "");
     47         printf("\n");
     48     }
     49 
     50     return 0;
     51 }
     52 
     53 void convert(long n, char *suffix)
     54 {
     55     if (n > 19)
     56     {
     57         printf("%s%s", ten[n / 10], one[n % 10]);
     58     }
     59     else
     60     {
     61         printf("%s", one[n]);
     62     }
     63 
     64     if (n > 0)
     65     {
     66         printf("%s", suffix);
     67     }
     68 }