Wednesday, 9 December 2015

Program to covert decimal number to Roman number representation

Covert decimal number to Roman number representation

The main concept behind this conversion is the key values for the Roman numbers which are 1000,900,500,400,100,90,50,40,10,9,5,4,1 and Roman symbol for each key, now we need to know the occurrences of each key in the number and replace it with it's respective Roman symbol.

Here's the simple code:

void decToRoman(int num){
    int val[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; // These are the key value in Roman counting
    char * symbols[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; //Symbols for key values
    int keyCount,i = 0;                  
    while (num){    
        keyCount=num/val[i];     //the number of times key value will appear in the Roman representation             
        while (keyCount--){      
            printf("%s",symbols[i]); 
        }
        num%=val[i];
        i++;                     //repeat the method with the next key value
    }
}
int main(){
int num;
printf("Enter a decimal number: ");
scanf("%d",&num);
printf("The Roman number representation of the number is: ");
decToRoman(num);
return 1;
}


Sample Output:
program-to-convert-decimal-number-to-Roman-number

No comments:

Post a Comment