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.

10 comments:

Unknown 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;
}

Unknown 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++;
}

Unknown 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();
}

tahuhu said...

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

tahuhu said...

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

Unknown said...

#include
int main()
{
int n,a=1;
printf("entetr the num.");

do
{
scanf("%d",&n);
while(n>=1000 || n>=900)
{
if(n>=1000)
printf("M",n=n-1000);
else if(n>=900)
printf("CM",n=n-900);
}
while(n>=500 || n>=400)
{
if(n>=500)
printf("D",n=n-500);
else if(n>=400)
printf("CD",n=n-400);
}
while(n>=100 || n>=90)
{
if(n>=100)
printf("C",n=n-100);
else if(n>=40)
printf("XC",n=n-40);
}
while(n>=50 || n>=40)
{
if(n>=50)
printf("L",n=n-50);
else if(n>=40)
printf("XL",n=n-40);
}
while(n>=10 || n>=9)
{
if(n>=10)
printf("X",n=n-10);
else if(n>=9)
printf("IX",n=n-9);
}
while(n>=5 || n>=4)
{
if(n>=5)
printf("V",n=n-5);
else if(n>=4)
printf("IV",n=n-4);
}
while(n>=1)
printf("I",n=n-1);
scanf("%d",&a);
}
while(a!=0);
return 0;
}