Categories: Java Java Programming & Coding

convert integer number to roman number in java

package r4r.in;

public class RomanToNumeral3 {

public static void main(String[] args) {

// convert integer number to roman number in java

for (int i = 1; i < 256; i++) {

System.out.println("i=" + i + " -> " + intToRoman(i));

}

}

public static String intToRoman(int num) {

StringBuilder sb = new StringBuilder();

int times = 0;

String[] romans = new String[] { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" };

int[] ints = new int[] { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };

for (int i = ints.length - 1; i >= 0; i--) {

times = num / ints[i];

if (times != 0) {

//System.err.println(times);

num %= ints[i];

while (times > 0) {

sb.append(romans[i]);

times--;

}

}

}

return sb.toString();

}

}


R4R.in Team
The content on R4R.in website is created by expert teams.
|| Blockchain || Cisco CCNA || Selenium || Internet security || Thermostat || Fashion || Online Games || Working From Home(WFH) || COVID-19 || Games || Cryptocurrency || Digital economy || Property || Sundarban National Park || NEET Course || Investment || Java || Inverter battery || PDF Drive || Online Library || Yoga || Career || Sports || SEO || Software Engineering || APPs || Digital Marketing || Certified True Copy || Mobiles || Software Testing || Books || TAX || Foreign Currency || AutoCAD || Online Course || Machine || Python || Banks || Bitcoin || Education || German Language || Festivals || Health ||