11616 - Roman Numerals
Solution Description : Number conversion problem Very easy problem but you need to spend time to code If input is decimal format then convert to Roman If input is Roman format then convert to Decimal In Decimal to Roman conversion You need to check If n>=1000 -> add 'M' m=(n/1000) times & n = n - m*1000 If n>=900 -> add 'CM' & n = n - 900 If n>=500 -> add 'D' & n = n - 500 If n>=400 -> add 'CD' & n = n - 400 If n>=100 -> add 'C' m=(n/100) times & n = n - m*100 If n>=90 -> add 'XC' & n = n - 90 If n>=50 -> add 'L' & n = n - 50 If n>=40 -> add 'XL' & n = n - 40 If n>=10 -> add 'X' m=(n/10) times & n = n - m*10 If n==9 -> add 'IX' & n = n - 9 If n>=5 -> add 'V' & n = n - 5 If n==4 -> add 'IV' & n = n - 4 If n>=1 -> add 'I' m=(n/1) times & n = n - m*1 In Roman to Decimal conversion You need to check each character If ch=='C' then check next character ch2=='M' -> add 900 or ch2=='D' ->add 400 otherwise -> add 100 If ch=='X' then check next character ch2=='C' -> add 90 or ch2=='L' ->add 40 otherwise -> add 10 If ch=='I' then check next character ch2=='X' -> add 9 or ch2=='V' -> add 4 otherwise -> add 1 If ch=='M' -> add 1000 If ch=='D' -> add 500 If ch=='L' -> add 50 If ch=='V' -> add 5 |
||||||||||