Skip to content

Instantly share code, notes, and snippets.

@vocalinternet
Created October 8, 2022 17:02
Show Gist options
  • Save vocalinternet/aa2c67d1411b12483977ca3acda6039d to your computer and use it in GitHub Desktop.
Save vocalinternet/aa2c67d1411b12483977ca3acda6039d to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
//Function to retrun value of roman symbols
int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
}
return -1;
}
//Function to convert Roman Numerals to Integer
int romanToInt (string input)
{
int i, n, ans=0, p=0;
n = input.length()-1;
for( i=n; i>=0; i--)
{
if( value(input[i]) >= p)
ans = ans + value(input[i]);
else
ans = ans - value(input[i]);
p = value(input[i]);
}
return ans;
}
//Main Function
int main() {
string a;
int num;
cout << "Roman Numerals. Input your numeral: \n";
cin>>a;
cout<<romanToInt(a)<<"\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment