Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Roman_To_Integer.cpp #291

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Programming/C++/Roman_To_Integer/Roman_To_Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Program to convert Roman to numerials
#include <bits/stdc++.h>
using namespace std;

int romanToInteger(char c)
{
switch(c)
{
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 0;
}

int romanToInt(string s) {
int size = s.size();
if(size==0)
return 0;
int result = 0;
for(int i=1; i<size; i++)
{
if( romanToInteger(s[i]) > romanToInteger(s[i-1]) )
result -= romanToInteger(s[i-1]);
else
result += (romanToInteger(s[i-1]));
}
result += romanToInteger(s.back());
return result;
}

// Driver Program
int main()
{
// Considering inputs given are valid
string str = "MCMIV";
cout << "Integer form of Roman Numeral is "
<< romanToInt(str) << endl;

return 0;
}