fork download
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. class Ideone
  5. {
  6. /**
  7. Convert decimal number lying between 1-3999 to roman numerals
  8.  
  9. SYMBOL VALUE
  10. I 1
  11. II 2
  12. III 3
  13. IV 4
  14. V 5
  15. X 10
  16. L 50
  17. C 100
  18. D 500
  19. M 1000
  20. **/
  21. private static String roman(int n) {
  22.  
  23. String[] thousands ={"", "M", "MM", "MMM"};
  24. String[] hundreds={"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
  25. String[] tens={"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
  26. String[] ones={"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "L"};
  27.  
  28. return thousands[n/1000]+hundreds[(n%1000)/100]+tens[(n%100)/10]+ones[n%10];
  29. }
  30.  
  31. public static void main(String[] args) throws java.lang.Exception
  32. {
  33. Scanner in = new Scanner(System.in);
  34. int n = in.nextInt();
  35. System.out.println(roman(n));
  36. }
  37. }
Success #stdin #stdout 0.22s 60956KB
stdin
2960
stdout
MMCMLX