Here is a C++ program to convert hexadecimal to Roman number and vice versa.
C++ program to convert hexadecimal to Roman number
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
int number;
long i,decimal=0,hexa[20];
char input[20];
cout<<"Enter the number in Hexadecimal ";
gets(input);
for(i=0;input[i]!='\0';i++)
{
if(input[i]>='A')
hexa[i]=input[i]-55;
else if(input[i]>='a')
hexa[i]=input[i]-87;
else
hexa[i]=input[i]-48;
}
--i;
for(int y=0;i>=0;y++,i--)
decimal+=hexa[i]*pow(16,y);
number=decimal;
cout<<"Roman Number : ";
while(number>0)
{
if(number>=1000)
{
cout<<"M";
number-=1000;
}
else if(number>=900)
{
cout<<"CM";
number-=900;
}
else if(number>=500)
{
cout<<"D";
number-=500;
}
else if(number>=400)
{
cout<<"CD";
number-=400;
}
else if(number>=100)
{
cout<<"C";
number-=100;
}
else if(number>=90)
{
cout<<"XC";
number-=90;
}
else if(number>=50)
{
cout<<"L";
number-=50;
}
else if(number>=40)
{
cout<<"XL";
number-=40;
}
else if(number>=10)
{
cout<<"X";
number-=10;
}
else if(number>=9)
{
cout<<"IX";
number-=9;
}
else if(number>=5)
{
cout<<"V";
number-=5;
}
else if(number>=4)
{
cout<<"IV";
number-=4;
}
else if(number>=1)
{
cout<<"I";
number-=1;
}
}
return 0;
}
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
int number;
long i,decimal=0,hexa[20];
char input[20];
cout<<"Enter the number in Hexadecimal ";
gets(input);
for(i=0;input[i]!='\0';i++)
{
if(input[i]>='A')
hexa[i]=input[i]-55;
else if(input[i]>='a')
hexa[i]=input[i]-87;
else
hexa[i]=input[i]-48;
}
--i;
for(int y=0;i>=0;y++,i--)
decimal+=hexa[i]*pow(16,y);
number=decimal;
cout<<"Roman Number : ";
while(number>0)
{
if(number>=1000)
{
cout<<"M";
number-=1000;
}
else if(number>=900)
{
cout<<"CM";
number-=900;
}
else if(number>=500)
{
cout<<"D";
number-=500;
}
else if(number>=400)
{
cout<<"CD";
number-=400;
}
else if(number>=100)
{
cout<<"C";
number-=100;
}
else if(number>=90)
{
cout<<"XC";
number-=90;
}
else if(number>=50)
{
cout<<"L";
number-=50;
}
else if(number>=40)
{
cout<<"XL";
number-=40;
}
else if(number>=10)
{
cout<<"X";
number-=10;
}
else if(number>=9)
{
cout<<"IX";
number-=9;
}
else if(number>=5)
{
cout<<"V";
number-=5;
}
else if(number>=4)
{
cout<<"IV";
number-=4;
}
else if(number>=1)
{
cout<<"I";
number-=1;
}
}
return 0;
}
C++ program to convert Roman to hexadecimal number
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int deci(char c)
{
switch(c)
{
case 'M':
case 'm':
return 1000;
case 'D':
case 'd':
return 500;
case 'C':
case 'c':
return 100;
case 'L':
case 'l':
return 50;
case 'X':
case 'x':
return 10;
case 'V':
case 'v':
return 5;
case 'I':
case 'i':
return 1;
}
}
int main()
{
int i=0,decimal=0;
long hexa[20],num,temp;
char rom[20];
cout<<"Enter roman number ";
gets(rom);
while(rom[i]!='\0')
{
if(deci(rom[i])<deci(rom[i+1]))
{
decimal+=deci(rom[i+1])-deci(rom[i]);
i+=2;
}
else
{
decimal+=deci(rom[i]);
i++;
}
}
num=decimal;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
cout<<"Hexadecimal equivalent : ";
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
return 0;
}
#include<math.h>
#include<stdio.h>
using namespace std;
int deci(char c)
{
switch(c)
{
case 'M':
case 'm':
return 1000;
case 'D':
case 'd':
return 500;
case 'C':
case 'c':
return 100;
case 'L':
case 'l':
return 50;
case 'X':
case 'x':
return 10;
case 'V':
case 'v':
return 5;
case 'I':
case 'i':
return 1;
}
}
int main()
{
int i=0,decimal=0;
long hexa[20],num,temp;
char rom[20];
cout<<"Enter roman number ";
gets(rom);
while(rom[i]!='\0')
{
if(deci(rom[i])<deci(rom[i+1]))
{
decimal+=deci(rom[i+1])-deci(rom[i]);
i+=2;
}
else
{
decimal+=deci(rom[i]);
i++;
}
}
num=decimal;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
cout<<"Hexadecimal equivalent : ";
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
return 0;
}
No comments:
Post a Comment