top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Decimal to Roman Conversion and Roman to Decimal Conversion

+1 vote
611 views

Roman Number Representation

Largest Symbol in Roman Numerals is M which represents 1000 and least is the I which means 1. Following is the list of Roman Numerals along with the equivalent value.

I   1
V   5
X   10
L   50
C   100
D   500
M   1,000

There is no zero in the roman numeral and a number is complete with pre or post value for example VI is 6 and IV is 4, similarly XI is 11 where as IX is 9. However prevalue can contain only one I and only one prevalue can be applied.

Decimal To Roman Conversion

#include<stdio.h>

char roman_Number[1000];
int length=0;

int main(){

    int j;
    long int decimal_number;

    printf("Enter any decimal number: ");
    scanf("%d",&decimal_number);

    // Roman number can not be negative
    if(decimal_number <= 0){
         printf("Invalid decimal number");
         return 0;
    }

    while(decimal_number != 0){

         if(decimal_number >= 1000){
             post_value('M',decimal_number/1000);
             decimal_number = decimal_number - (decimal_number/1000) * 1000;
         }
         else if(decimal_number >=500){
             if(decimal_number < (500 + 4 * 100)){
                 post_value('D',decimal_number/500);
                 decimal_number = decimal_number - (decimal_number/500) * 500;
             }
             else{
                 pre_value('C','M');
                 decimal_number = decimal_number - (1000-100);
             }
         }
         else if(decimal_number >=100){
             if(decimal_number < (100 + 3 * 100)){
                 post_value('C',decimal_number/100);
                 decimal_number = decimal_number - (decimal_number/100) * 100;
             }
             else{
                 pre_value('L','D');
                 decimal_number = decimal_number - (500-100);
             }
         }
         else if(decimal_number >=50){
             if(decimal_number < (50 + 4 * 10)){
                 post_value('L',decimal_number/50);
                 decimal_number = decimal_number - (decimal_number/50) * 50;
             }
             else{
                 pre_value('X','C');
                 decimal_number = decimal_number - (100-10);
             }
         }
         else if(decimal_number >=10){
             if(decimal_number < (10 + 3 * 10)){
                 post_value('X',decimal_number/10);
                 decimal_number = decimal_number - (decimal_number/10) * 10;
             }
             else{
                 pre_value('X','L');
                 decimal_number = decimal_number - (50-10);
             }
         }
         else if(decimal_number >=5){
             if(decimal_number < (5 + 4 * 1)){
                 post_value('V',decimal_number/5);
                 decimal_number = decimal_number - (decimal_number/5) * 5;
             }
             else{
                 pre_value('I','X');
                 decimal_number = decimal_number - (10-1);
             }
         }
         else if(decimal_number >=1){
             if(decimal_number < 4){
                 post_value('I',decimal_number/1);
                 decimal_number = decimal_number - (decimal_number/1) * 1;
             }
             else{
                 pre_value('I','V');
                 decimal_number = decimal_number - (5-1);
             }
         }
    }

    printf("Equivalent Roman number is - ");
    for(j=0;j<length;j++)
         printf("%c",roman_Number[j]);

    return 0;

}

void pre_value(char c1,char c2){
    roman_Number[length++] = c1;
    roman_Number[length++] = c2;
}

void post_value(char c,int n){
    int j;
    for(j=0;j<n;j++)
         roman_Number[length++] = c;

}

Roman to Decimal Conversion

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

void main()
{
    char roman_Number[1000];
    int index=0;
    long int decimal_number =0;

    printf("Enter any roman number (allowed digits are I, V, X, L, C, D, M):  \n");
    scanf("%s", roman_Number);

    while(roman_Number[index]){

         if(decimal_value(roman_Number[index]) < 0)
     {
             printf("Invalid roman digit entered : %c",roman_Number[index]);
             return;
         }

         if((strlen(roman_Number) -index) > 2)
     {
         // Large should appear left with exception of one left 
             if(decimal_value(roman_Number[index]) < decimal_value(roman_Number[index+2])) 
         {
                 printf("Invalid roman number");
                 return;
             }
         }

         if(decimal_value(roman_Number[index]) >= decimal_value(roman_Number[index+1]))
             decimal_number = decimal_number + decimal_value(roman_Number[index]);
         else
     {
             decimal_number = decimal_number + (decimal_value(roman_Number[index+1]) - decimal_value(roman_Number[index]));
             index++;
         }
         index++;
    }

    printf("Its decimal value is : %ld", decimal_number);
}

int decimal_value(char c){

    int local_value=0;

    switch(c){
         case 'I': 
         local_value = 1; 
         break;

     case 'V': 
         local_value = 5; 
         break;

     case 'X': 
         local_value = 10; 
         break;

     case 'L': 
         local_value = 50; 
         break;

     case 'C': 
         local_value = 100; 
         break;

     case 'D': 
         local_value = 500; 
         break;

     case 'M': 
         local_value = 1000; 
         break;

     case '\0': 
         local_value = 0; 
         break; 

     default: 
         local_value = -1; 
    }

    return local_value;
}
posted Nov 4, 2014 by anonymous

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...