Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Convert Integer number to Roman Number

Last Updated on March 28, 2022 by Ria Pathak

Concepts Used:

Strings

Difficulty Level:

Medium

Problem Statement (Simplified):

For a given number between 0 and 4000, print out it’s Roman Form.

See original problem statement here

Test Case:

Input:
1
49

Output:
XLIX

Explanation:
Starting from left we have 4 at 10th digit place, hence it is 40, 40 can be written as XL in Roman Numerals.

Next, we have 9 at unit digit place which can be written as IX in Roman Numerals.

So, our final answer is XLIX. 

Solving Approach :

1) Roman numerals with their Integer counterpart are as follows :

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

2) We scan number from left to right digit by digit and print digit’s respective value depending on its unit place whether 1000th unit place, 100th unit place, 10th unit place, or unit place.

3) We follow the given chart for printing values according to their place in the number.

Digit Unit Place Tenth Place Hundredth Place Thousandth Place
1 I X C M
2 II XX CC MM
3 III XXX CCC MMM
4 IV XL CD
5 V L D
6 VI LX DC
7 VII LXX DCC
8 VIII LXXX DCCC
9 IX XC CM

Example:

  • Let’s take 2975 as an example, we scan elements from left to right digit by digit.

  • At 1st place we have, 2 at Thousandth digit place, which means we have 2000 in number, so 2000 can be written as MM in Roman Numerals.

  • At 2nd place we have, 9 at Hundredth digit place, which means we have 900 in number, so 900 can be written as CM in Roman Numerals.

  • At 3rd place we have, 7 at Tenth digit place, which means we have 70 in number, so 70 can be written as LXX in Roman Numerals.

  • At last place we have, 5 at Thousandth digit place, which means we have 5 in number, so 5 can be written as V in Roman Numerals.

  • Finally, we concatenate all of them, so 2975 can be written as MMCMLXXV

Solutions:

#include <stdio.h>

int main()
{
  int test;
  scanf("%d",&test);

  while(test--){

    int n;
    scanf("%d",&n);

    char val[1001][2];
    val[1][0] = 'I';
    val[5][0] = 'V';
    val[10][0] = 'X';
    val[50][0] = 'L';
    val[100][0] = 'C';
    val[500][0] = 'D';
    val[1000][0] = 'M';

    int tens = 1;
    int k = 0;
    char output[100];

    //Counting digits
    for(int i=n; i; i/=10)
        tens *= 10;

    tens/=10;

    while(tens!=0){
      int digit = n/tens;
      if( digit < 4){
        for(int i=0; i<digit; i++)
          output[k++] = val[tens][0];
      }
      else if( digit == 4){
        output[k++] = val[tens][0];
        output[k++] = val[tens*5][0];
      }
      else if(digit == 5){
        output[k++] = val[tens*5][0];
      }
      else if(digit>5 && digit<9){
        output[k++] = val[tens*5][0];
        for(int i=0; i<digit-5; i++)
          output[k++] = val[tens][0];
      }
      else if(digit == 9){
        output[k++] = val[tens][0];
        output[k++] = val[tens*10][0];
      }

      n %= tens;
      tens /= 10;
    }

    output[k] = '\0';
    printf("%s\n", output);

  }

}
#include <bits/stdc++.h>
using namespace std;

int main()
{
  int test;
  cin>>test;

  while(test--){

    int n;
    cin>>n;

    char val[1001][2];
    val[1][0] = 'I';
    val[5][0] = 'V';
    val[10][0] = 'X';
    val[50][0] = 'L';
    val[100][0] = 'C';
    val[500][0] = 'D';
    val[1000][0] = 'M';

    int tens = 1;
    int k = 0;
    char output[100];

    //Counting digits
    for(int i=n; i; i/=10)
        tens *= 10;

    tens/=10;

    while(tens!=0){
      int digit = n/tens;
      if( digit < 4){
        for(int i=0; i<digit; i++)
          output[k++] = val[tens][0];
      }
      else if( digit == 4){
        output[k++] = val[tens][0];
        output[k++] = val[tens*5][0];
      }
      else if(digit == 5){
        output[k++] = val[tens*5][0];
      }
      else if(digit>5 && digit<9){
        output[k++] = val[tens*5][0];
        for(int i=0; i<digit-5; i++)
          output[k++] = val[tens][0];
      }
      else if(digit == 9){
        output[k++] = val[tens][0];
        output[k++] = val[tens*10][0];
      }

      n %= tens;
      tens /= 10;
    }
    output[k] = '\0';
    cout<<output<<endl;

  }
  return 0;
}
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
  public static void main(String args[]) throws IOException {

    Scanner sc= new Scanner(System.in);
    int test = sc.nextInt();
      while(test != 0){

        int n = sc.nextInt();

        String val = "";
        for(int i=0; i<1001; i++){
          if(i==1){
            val += 'I';
          }
          else if(i==5){
            val += 'V';
          }
          else if(i==10){
            val += 'X';
          }
          else if(i==50){
            val += 'L';
          }
          else if(i==100){
            val += 'C';
          }
          else if(i==500){
            val += 'D';
          }
          else if(i==1000){
            val += 'M';
          }
          else{
            val += ' ';
          }
        }

        int tens = 1;
        String output = "";

        //Counting digits
        for(int i=n; i!=0 ; i/=10)
            tens *= 10;

        tens/=10;

        while(tens!=0){
          int digit = n/tens;
          if( digit < 4){
            for(int i=0; i<digit; i++)
              output += val.charAt(tens);
          }
          else if( digit == 4){
            output += val.charAt(tens);
            output += val.charAt(tens*5);;
          }
          else if(digit == 5){
            output += val.charAt(tens*5);
          }
          else if(digit>5 && digit<9){
            output += val.charAt(tens*5);
            for(int i=0; i<digit-5; i++)
              output += val.charAt(tens);
          }
          else if(digit == 9){
            output += val.charAt(tens);
            output += val.charAt(tens*10);
          }

          n %= tens;
          tens /= 10;
        }

        System.out.println(output);
        test--;
      }

  }
}

[forminator_quiz id="1008"]

This article tried to discuss Strings. Hope this blog helps you understand and solve the problem. To practice more problems on Strings you can check out MYCODE | Competitive Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *