From fa8304323e0e83417d02341f75684388d424b7a7 Mon Sep 17 00:00:00 2001 From: RAHUL CHOUDHARY <111217626+RJSHIVA@users.noreply.github.com> Date: Fri, 27 Oct 2023 07:07:17 +0530 Subject: [PATCH] Create Integer to Roman --- algorithms/Java/strings/Integer to Roman | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 algorithms/Java/strings/Integer to Roman diff --git a/algorithms/Java/strings/Integer to Roman b/algorithms/Java/strings/Integer to Roman new file mode 100644 index 00000000..c689abd4 --- /dev/null +++ b/algorithms/Java/strings/Integer to Roman @@ -0,0 +1,31 @@ +class Solution { + public String intToRoman(int num) { + + // Initialize arrays for integer values and corresponding Roman numeral symbols + int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + String[] romanNumerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + + // Create a StringBuilder object to store the result string + StringBuilder sb = new StringBuilder(); + + // Initialize index variable + int i = 0; + + // Iterate until the given number becomes zero + while (num > 0) { + // Check if the current value can be subtracted from the given number + if (num >= values[i]) { + // Append the corresponding Roman numeral symbol to the result string + sb.append(romanNumerals[i]); + // Subtract the value from the number + num -= values[i]; + } else { + // Move on to the next smaller value + i++; + } + } + + // Return the result string + return sb.toString(); + } +}