This blog is under construction

Friday 5 July 2013

C program to convert hexadecimal to roman

Please check the below link to know how to write roman numerals.

Write a C program to convert hexadecimal value to roman equivalent.


  #include <stdio.h>
  #include <math.h>
  #include <string.h>

  /* converts given decimal value to roman equivalent */
  void decimalToRoman(int val) {
        printf("Roman Equivalent: ");
        while (val > 0) {
                if (val >= 1000) {
                        /* M - 1000 */
                        printf("M");
                        val = val - 1000;
                } else if (val >= 500) {
                        /*
                         * D is 500. CM is 900
                         * CM = M - C = 1000 - 100 => 900
                         */
                        if (val >= 900) {
                                printf("CM");
                                val = val - 900;
                        } else {
                                printf("D");
                                val = val - 500;
                        }
                } else if (val >= 100) {
                        /*
                         * C is 100. CD is 400
                         * CD = D - C = 500 - 100 => 400
                         */
                        if (val >= 400) {
                                printf("CD");
                                val = val - 400;
                        } else {
                                printf("C");
                                val = val - 100;
                        }
                } else if (val >= 50) {
                        /*
                         * L is 50. XC is 90
                         * XC = C - X = 100 - 10 => 90
                         */
                        if (val >= 90) {
                                printf("XC");
                                val = val - 90;
                        } else {
                                printf("L");
                                val = val - 50;
                        }
                } else if (val >= 9) {
                        /*
                         * XL is 40. IX is 9. X is 10
                         * XL = L - X = 50 - 10 = 40
                         * IX = X - I = 10 - 1 = 9
                         */
                        if (val >= 40) {
                                printf("XL");
                                val = val - 40;
                        } else if (val == 9) {
                                printf("IX");
                                val = val - 9;
                        } else {
                                printf("X");
                                val = val - 10;
                        }
                } else if (val >= 4) {
                        /*
                         * V is 5 and IV is 4
                         * IV = V - I = 5 - 1 => 4
                         */
                        if (val >= 5) {
                                printf("V");
                                val = val - 5;
                        } else {
                                printf("IV");
                                val = val - 4;
                        }
                } else {
                        printf("I");
                        val = val - 1;
                }
        }
        printf("\n");
  }


  int main () {
        char data[10], ch;
        int value = 0, power = 0, i = 0, j = 0;

        /* get the hexadecimal value from the user */
        printf("Enter your hexadecimal value: 0x");
        fgets(data, 10, stdin);
        data[strlen(data) - 1] = '\0';

        /* convert hexadecimal value to decimal */
        for (i = strlen(data); i > 0; i--) {
                ch = data[j++];
                power = pow(16, i - 1);
                if (ch >='0'&& ch <= '9') {
                        /*
                         * data character is any value from 0 to 9
                         */
                        value = value + (ch - '0') * power;
                } else if (ch >= 'a' && ch <= 'f') {
                        /*
                         * data character is any value from a to f
                         * a is 10, b is 11... F is 15 - conversion
                         */
                        value = value + (ch - 'a' + 10) * power;
                } else if (ch >= 'A' && ch <= 'F') {
                        /*
                         * data character is any value from A to F
                         * A is 10, B is 11.. F is 15 - conversion
                         */
                        value = value + (ch - 'A' + 10) * power;
                } else {
                        /*
                         * valid hexadecimal values 0-9, a-f, A-F
                         * if data doesn't belong to any of the
                         * above values, then its a wrong data
                         */
                        printf("Wrong Input!!!\n");
                        return (-1);
                }
        }
        decimalToRoman(value);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your hexadecimal value: 0x100
  Roman Equivalent: CCLVI



No comments:

Post a Comment