C program to convert Roman number to decimal number





C code for roman numbers to English numbers

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

int digitValue(char);

int main(){

    char roman_Number[1000];
    int i=0;
    long int number =0;
   
    printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M):  \n");
    scanf("%s",roman_Number);
   
    while(roman_Number[i]){
        
         if(digitValue(roman_Number[i]) < 0){
             printf("Invalid roman digit : %c",roman_Number[i]);
             return 0;
         }
            
         if((strlen(roman_Number) -i) > 2){
             if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
                 printf("Invalid roman number");
                 return 0;
             }
         }

         if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))
             number = number + digitValue(roman_Number[i]);
         else{
             number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
             i++;
         }
         i++;
    }
        
    printf("Its decimal value is : %ld",number);
   
    return 0;

}

int digitValue(char c){

    int value=0;

    switch(c){
         case 'I': value = 1; break;
         case 'V': value = 5; break;
         case 'X': value = 10; break;
         case 'L': value = 50; break;
         case 'C': value = 100; break;
         case 'D': value = 500; break;
         case 'M': value = 1000; break;
         case '\0': value = 0; break;
         default: value = -1; 
    }

    return value;
}

Sample output:

Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XVII
Its decimal value is: 17




6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

9 comments:

Priyank Namdev said...

pls give some description of this program.

Srikanth CHindam said...
This comment has been removed by the author.
Srikanth CHindam said...
This comment has been removed by the author.
fedillah koh said...

Using c language, the input and output are 0 and 1. Its output is identical to the input in the even position but inverted in the odd position. For example, input 0000111 it output 1010010. How to do this ?

Srikanth CHindam said...

Check This Code
may be it is simpler than your code

#include
#include
int main()
{
int a[10],len,i,j,val;
char rom[20];
printf("Enter the Roman Numeral:");
scanf("%s",rom);
printf("%s",rom);
i=0;
while(rom[i]!='\0')
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
printf("\n %d %d",a[i],i);
i++;
}

len=strlen(rom);
printf("\n %d\n",len);
for(i=len-1;i>=0;i--)
{
if(i!=0)
{
if(a[i]>a[i-1])
a[i-1]=a[i]-a[i-1];
else
val=val+a[i];
}
else
val=val+a[i];
}
printf("Its Decimal Equivalent is: %d",val);
return 0;
}

Jenei Csilla said...

Could you explain this part for me?
if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))
number = number + digitValue(roman_Number[i]);
else{
number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
i++;
}
i++;
}

Ryan Druhan said...

This is probably the shortest/simplest solution to this problem

#include < stdio.h >

void main(){
char ROMAN[][3] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int NUM[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
char result[20] = { 0 };
int num, i;

printf("Enter a Number: ");
scanf("%i", &num);
printf("\nRoman Numeral.: ");
for(i = 0; num > 0 && num < 3999; i++)
while(num/NUM[i]){
printf("%s", ROMAN[i]);
num -= NUM[i];
}
getchar();
}

Unknown said...

is it possible to have one that starts with
int romanToInt(char *s){
...
//and ends with
...
return -1;
} /* romanToInt */

Unknown said...

is it possible to have one that starts with
int romanToInt(char *s){
...
//and ends with
...
return -1;
} /* romanToInt */