Created
January 26, 2017 00:40
-
-
Save khayyamsaleem/fa9f28f558e7c5ad4668584c2969f7e3 to your computer and use it in GitHub Desktop.
converts integers to roman numeral representation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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