Algorithms icon indicating copy to clipboard operation
Algorithms copied to clipboard

Integer to Roman conversion in C, C++,Python

Open hlw-aryan opened this issue 1 year ago • 4 comments

The problem is to implement a function that converts an integer to its corresponding Roman numeral representation.

I would like to implement a function int_to_roman that takes an integer as input and returns its Roman numeral representation as a string. The function should follow the rules of Roman numeral representation, where certain symbols are used to represent specific values. For example, the integer 3 should be converted to "III", the integer 4 should be converted to "IV", and so on.

Solutions

  • One of the solution is to use a lookup table that maps integer values to their corresponding Roman numeral symbols. The function can then iterate over the lookup table in descending order of values and append the corresponding symbols to the result string until the input integer is fully converted.
  • Another alternative solution is to use a series of if-else or switch statements to handle specific cases of integer values and their corresponding Roman numeral symbols. This approach can be more straightforward but may require more lines of code.

Additional context It's important to handle edge cases such as negative numbers or numbers outside the valid range of Roman numerals.

@Kumar-laxmi Please review the proposed implementation and provide feedback or suggestions for improvement.

hlw-aryan avatar Jun 01 '23 10:06 hlw-aryan