Skip to content

Instantly share code, notes, and snippets.

@khayyamsaleem
Created January 26, 2017 00:40
Show Gist options
  • Save khayyamsaleem/fa9f28f558e7c5ad4668584c2969f7e3 to your computer and use it in GitHub Desktop.
Save khayyamsaleem/fa9f28f558e7c5ad4668584c2969f7e3 to your computer and use it in GitHub Desktop.
converts integers to roman numeral representation
import java.util.Scanner;
public class romanNum {
public static String convert(int num){
int[] nums = new int[] {1000, 900, 500, 400, 100,
90, 50, 40, 10, 9, 5, 4, 1};
String[] letters = new String[] { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" };
String out = "";
int times = 0;
for(int i = 0; i < nums.length; i++){
times = num / nums[i];
num %= nums[i];
while(times > 0){
out += letters[i];
times--;
}
}
return out;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Integer number (1-3999): ");
int number = input.nextInt();
System.out.println("Roman numeral: " + convert(number));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment