fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  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. string roman(int n){
  22. vector<int> num={1,4,5,9,10,40,50,90,100,400,500,900,1000};
  23. vector<string> romann={"I","IV","V","IX","X","XC","L","XC","C","CD","D","CM","M"};
  24. int i=12;
  25. string ans="";
  26. while(n>0){
  27. int div=n/num[i];
  28. n=n%num[i];
  29.  
  30. while(div--){
  31. ans+=romann[i];
  32. }
  33. i--;
  34. }
  35. return ans;
  36. }
  37.  
  38. int main() {
  39. int intNum = 0;
  40. cin >> intNum;
  41. cout << roman(intNum);
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5276KB
stdin
3999
stdout
MMMCMXCIX