Advertisement
dmilicev

converting_arabic_numerals_to_roman_numerals.c

Oct 29th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /*
  2.  
  3.     converting_arabic_numerals_to_roman_numerals.c
  4.  
  5.  
  6.     As a reminder, the following tables contain Roman numerals:
  7.  
  8.     I   V   X   L   C   D   M
  9.     1   5   10  50  100 500 1000
  10.  
  11.     IV  IX  XL  XC  CD  CM
  12.     4   9   40  90  400 900
  13.  
  14.     Test case:
  15.  
  16.     2736 = MMDCCXXXVI
  17.  
  18.     or separated:
  19.  
  20.            2  7   3   6
  21.     2736 = MM DCC XXX VI
  22.  
  23.            3   4  4   4
  24.     3444 = MMM CD XL  IV
  25.  
  26.            3   9  9   9
  27.     3999 = MMM CM XC  IX
  28.  
  29. MDCCCLXXXVIII = 1000 + 500 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 5 + 1 + 1 + 1 = 1888
  30.  
  31. If the value of the left digit is less than the right,
  32. then the value of the left digit is subtracted from the value of the right.
  33.  
  34. MCMXCIX = M CM XC IX or 1000 + (1000 - 100) + (100 - 10) + (10 - 1) = 1999
  35.  
  36. MCMXCIX = M CM XC IX or 1000 - 100 + 1000 - 10 + 100 - 1 + 10 = 1999
  37.  
  38.  
  39.     I   V   X   L   C   D   M
  40.     1   5   10  50  100 500 1000
  41.  
  42.  
  43.     The number of thousands: the number of Ms at the beginning * 1000 is the number of thousands.
  44.  
  45.     0       1000        2000        3000    4000    5000    ...     11000
  46.     0       1           2           3       4       5       ...     11
  47.             M           MM          MMM     MMMM    MMMMM           MMMMMMMMMMM
  48.  
  49.     Number of hundreds:       <4 , =4 , >4
  50.  
  51.     0       100     200     300     400     500     600     700     800     900
  52.     0       1       2       3       4       5       6       7       8       9
  53.             C       CC      CCC     CD      D       DC      DCC     DCCC    CM
  54.  
  55.  
  56.     Number of tens:           <4 , =4 , >4
  57.  
  58.     0       10      20      30      40      50      60      70      80      90
  59.     0       1       2       3       4       5       6       7       8       9
  60.             X       XX      XXX     XL      L       LX      LXX     LXXX    XC
  61.  
  62.  
  63.     Number of ones            <4 , =4 , >4
  64.  
  65.     0       1       2       3       4       5       6       7       8       9
  66.             I       II      III     IV      V       VI      VII     VIII    IX
  67.  
  68.  
  69. */
  70.  
  71. #include <stdio.h>
  72. #include <string.h>
  73.  
  74.  
  75. void arabic2roman(int arabic, char *roman){
  76.  
  77.     // Values of roman digits
  78.     int vrednost[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  79.  
  80.     // Simbols of roman digits
  81.     char* simbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  82.  
  83.     int i = 0;
  84.  
  85.     strcpy(roman,"");                  // we empty the string roman
  86.  
  87.     while (arabic){                    // while the Arabic number is non-zero
  88.         while (arabic/vrednost[i]){    // while arabic contains the highest possible value of Roman digits
  89.             strcat(roman, simbol[i]);  // we add a symbol for this value to a Roman string
  90.             arabic -= vrednost[i];     // we reduce the arabic number by the value of the Roman digit added
  91.         }
  92.         i++;                            // we move on to the next value of the Roman digit
  93.     }
  94. }
  95.  
  96.  
  97. int main(void) {
  98.  
  99.     //int arabic=11000;
  100.     int arabic=2736;
  101.     char roman[100];
  102.  
  103.     printf("\n arabic to roman \n");
  104.  
  105.     arabic2roman(arabic, roman );
  106.  
  107.     printf("\n %d = %s \n", arabic, roman );
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement