Simple C++ Solution:: If-else

string Solution::intToRoman(int A) {
string s;
int m = A/1000;
for(int i=1;i<=m;i++)
{
s += “M”;
}
int n = A%1000;
n = n/100;
if(n <4)
{
for(int i=1;i<=n;i++)
{
s+= “C”;
}
}
else if(n==4)
{
s+= “CD”;
}
else if(n==5)
{
s+=“D”;
}
else if(n>5 and n<9)
{
s+=“D”;
for(int i=1;i<=(n-5);i++)
{
s+= “C”;

    }
}
else if(n==9)
{
    s+="CM";
}

int p = A%100;
p = p/10;
if(p>=1 and p<4)
{
    for(int i=1;i<=p;i++)
    {
        s+="X";
    }
}
else if(p==4)
{
    s+="XL";
}
else if(p==5)
{
    s+="L";
}
else if(p>5 and p<9)
{
    s+="L";
    for(int i=1;i<=(p-5);i++)
    {
        s+="X";
    }
}
else if(p==9)
{
    s+="XC";
}
 int q = A%10;
 if(q>=1 and q<4)
 {
     for(int i=1;i<=q;i++)
     {
         s+="I";
     }
 }
 if(q==4)
 {
     s+="IV";
 }
 if(q==5)
 {
     s+="V";
 }

 if(q>5 and q<9)
 {
     s+="V";
     for(int i=1;i<=(q-5);i++)
     {
         s+="I";
     }
 }
 if(q==9)
 {
     s+="IX";
 }

 return s;

}

Click here to start solving coding interview questions