training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

dec2rom2.c (1014B)


      1 /* Esercizio 4.33 */
      2  
      3 #include <stdio.h>
      4 
      5 int main()
      6 {
      7    int i, num = 1984;
      8 
      9    /* migliaia */
     10    for(i = num / 1000; i >= 1; --i) {
     11       printf("M");
     12       num -= 1000;
     13    }
     14 
     15    /* cinque-centinaia */
     16    if (num / 500) {
     17       printf("D");
     18       num -= 500;
     19    }
     20 
     21    /* centinaia */
     22    for(i = num / 100; i >= 1; --i) {
     23 
     24       if (i == 4) {
     25 	 printf("CD");
     26 	 num -= 400;
     27 	 break;
     28       }
     29       printf("C");
     30       num -= 100;
     31    }
     32 
     33    /* stampo le decine */
     34    if (num / 10 >= 5) {
     35       printf("L");
     36       num -= 50;
     37    }
     38 
     39    /* stampo il resto delle decine */
     40    for(i = num / 10; i >= 1; --i) {
     41       if (i == 4) {
     42 	 printf("XL");
     43 	 num -= 40;
     44 	 break;
     45       }
     46       printf("X");
     47       num -= 10;
     48    }
     49 
     50    /* cinquetti */
     51    if (num >= 5) {
     52       printf("V");
     53       num -= 5;
     54    }
     55 
     56    for(i = num ; i >= 1 ; --i) {
     57       if (i == 4) {
     58 	 printf("IV");
     59 	 num -= 4; /* not required */
     60 	 break;
     61       }
     62       printf("I");
     63       num -= 1; /* not required */
     64    }
     65 
     66    printf("\n");
     67 
     68    return 0;
     69 } /* E0F main */
     70