C program code to convert roman numeral into decimal number.
In this tutorial we will learn how to write a c program code that convert roman number to decimal number.
In this program we will enter a Roman number then it converts into Decimal number.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int a[100],len,x,y,z;
char rom[100];
printf("\n Enter the Roman Numeral for conveting to decimal:\n ");
scanf("%s",rom);
len=strlen(rom);
for(x=0; x<len; x++)
{
if(rom[x]=='I')
a[x]=1;
else if(rom[x]=='V')
a[x]=5;
else if(rom[x]=='X')
a[x]=10;
else if(rom[x]=='L')
a[x]=50;
else if(rom[x]=='C')
a[x]=100;
else if(rom[x]=='D')
a[x]=500;
else if(rom[x]=='M')
a[x]=1000;
else
{
printf("\n Invalid Value.");
exit(0);
}
}
z=a[len-1];
for(x=len-1; x>0; x--)
{
if(a[x]>a[x-1])
z=z-a[x-1];
else if(a[x]==a[x-1] || a[x]<a[x-1])
z=z+a[x-1];
}
printf("\n Its Decimal Equivalent is:\n ");
printf("%d\n",z);
return 0;
}
In this output we can see that it converts Roman number into Decimal number.
If we will enter the roman number as XXVII.
Then its convert this Roman number into Decimal number as 27.
In this tutorial we will learn how to write a c program code that convert roman number to decimal number.
In this program we will enter a Roman number then it converts into Decimal number.
Program Description:
This program is a c program that convert Roman number to Decimal number.It takes input Roman number and convert it into Deciaml number.
We will use capital letters for Roman number.C program code
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int a[100],len,x,y,z;
char rom[100];
printf("\n Enter the Roman Numeral for conveting to decimal:\n ");
scanf("%s",rom);
len=strlen(rom);
for(x=0; x<len; x++)
{
if(rom[x]=='I')
a[x]=1;
else if(rom[x]=='V')
a[x]=5;
else if(rom[x]=='X')
a[x]=10;
else if(rom[x]=='L')
a[x]=50;
else if(rom[x]=='C')
a[x]=100;
else if(rom[x]=='D')
a[x]=500;
else if(rom[x]=='M')
a[x]=1000;
else
{
printf("\n Invalid Value.");
exit(0);
}
}
z=a[len-1];
for(x=len-1; x>0; x--)
{
if(a[x]>a[x-1])
z=z-a[x-1];
else if(a[x]==a[x-1] || a[x]<a[x-1])
z=z+a[x-1];
}
printf("\n Its Decimal Equivalent is:\n ");
printf("%d\n",z);
return 0;
}
OUTPUT:
In this output we can see that it converts Roman number into Decimal number.
If we will enter the roman number as XXVII.
Then its convert this Roman number into Decimal number as 27.
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment