problem no 54:convert an integer to roman.

 question:

                        Convert integer to Roman No
Basic Accuracy: 49.9% Submissions: 14164 Points: 1

Given an integer n, your task is to complete the function convertToRoman which prints the corresponding roman number of n. Various symbols and their values are given below.

I 1
V 5
X 10
L 50
C 100
D 500
M 1000

 

Example 1:

Input:
n = 5
Output: V

 

Example 2:

Input:
n = 3
Output: III

 

Your Task:
Complete the function convertToRoman() which takes an integer N as input parameter and returns the equivalent roman. 



ANSWER:

string convertToRoman(int number)
{
    string s="";
    int num[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
    string sym[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
    int i=12;   
    while(number>0)
    {
      int div = number/num[i];
      number = number%num[i];
      while(div--)
      {
         s=s+sym[i];
      }
      i--;
    }
    return s;
}

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.