Roman numers to decimal

  • File name: Roman to decimal
  • Size: 689 b
yes /

Download

Program that converts a roman numeral such as "XVIII" to decimal "33" by a function with a string with the roman numer.


We assume that can be put four equal characters instead of subtracting, ie will IIII instead of IV. Thus, it is sum values depending on which character is concerned.



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

int rom2dec(char s[]){
    int i,r=0,valor;

    for(i=0;i<strlen(s);i++){
        switch(s[i]){
            case 'I':valor=1;break;
            case 'V':valor=5;break;
            case 'X':valor=10;break;
            case 'L':valor=50;break;
            case 'C':valor=100;break;
            case 'D':valor=500;break;
            case 'M':valor=1000;break;
            default: break;
        }
        r+=valor;
    }
    return r;
}

int main(){
    char cad[20];
    int decimal;

    printf("Escribe un número romano\n:");
    scanf("%s",cad);
    decimal=rom2dec(cad);
    printf("El valor decimal de %s es %d",cad,decimal);

    return 0;
}

 

More exercises in https://www.youtube.com/channel/UCK05KbyByXPCMRJEqyL4VXw/videos

No ratings yet - be the first to rate this.

Add a comment

You're using an AdBlock like software. Disable it to allow submit.