Create Integer to Roman

pull/1279/head
RAHUL CHOUDHARY 2023-10-27 07:07:17 +05:30 committed by GitHub
parent d3c2184af8
commit fa8304323e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -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();
}
}