Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions roman to integer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Roman to Integer Converter

I have created a Java Program to convert Roman numbers to integers.
41 changes: 41 additions & 0 deletions roman to integer/file.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class Solution {
public int romanToInt(String A) {
int ans=0;
int i=0;
while(i<A.length()){
if(i+1<A.length()){
if(getValue(A.charAt(i+1))>getValue(A.charAt(i))){
ans+=getValue(A.charAt(i+1))-getValue(A.charAt(i));
i+=2;
}else{
ans+=getValue(A.charAt(i));
i++;
}
}else{
ans+=getValue(A.charAt(i));
i++;
}
}
return ans;
}
int getValue(char ch){
switch(ch){
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;
default:
return -1;
}
}
}