so there was a problem I had in implementing a program (given below). The problem occurs when I pass num = 1006, the private method returns "MVI", but the public method returns "M". Could anyone explain this?
public class Solution {
public String intToRoman(int num) {
String j = intToRoman(num, "");
return j;
}
private String intToRoman(int num, String s) {
//String s = "";
if (num >= 1000) {
s += 'M';
num -= 1000;
intToRoman(num, s);
} else if (num >= 500) {
s += 'D';
num -= 500;
intToRoman(num, s);
} else if (num >= 100) {
s += 'C';
num -= 100;
intToRoman(num, s);
} else if (num >= 50) {
s += 'L';
num -= 50;
intToRoman(num, s);
} else if (num >= 10) {
s += 'X';
num -= 10;
intToRoman(num, s);
} else if (num >= 5) {
s += 'V';
num -= 5;
System.out.println("Checkpoint 1");
System.out.println(s);
intToRoman(num, s);
} else if (num >= 1) {
s += 'I';
num -= 1;
System.out.println("Checkpoint 2");
intToRoman(num, s);
} else {
System.out.println("Returning "+ s);
return s;
}
return s;
}
return intToRomain(num, s);.s