Array ( [0] => [1] => questions [2] => Basic [3] => Decimal-Number-to-Roman-Number ) Basic | Decimal Number to Roman Number | THE INQUISITIVE





Decimal Number to Roman Number

LEVEL:Beginner

Description

Given a integer N. You have to convert the decimal number to roman number.

Input Format

First line contains a integer N.

Output Format

Print the corresponding roman number.


Example 1:

Input
3
Output
III
Example 2:

Input
8
Output
VIII
Example 3:

Input
17
Output
XVII

oops

Login to see Discussion




Approach

Approach 1: using while loop

Step-i) Create a empty string
Step-ii) compare the number with the standard number of roman as M for 1000, CM for 900.......... add it to string
Step-iii)after comparing the number reduce that number from the given number
Step-iv) Repeat ii and iii till number is greater than 0
Step-v) Return the string

Time Complexity: O(logn)
Space Complexity: It turns out that there isn't any exact notation for this to say as it varies as per number

Approach 2: Using division

Step-i) Create an empty string
Step-ii) compare the number with standard number of roman and get remainder by dividing given number with the number compared
Step-iii) Add respective roman number to the string remainder no of times
Step-iv) Repeat steps ii and iii till the given number is greater than zero
Step-v) Return the string

Time Complexity: O(logn)
Space Complexity: It turns out that there isn't any exact notation for this to say as it varies as per number


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution