ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT

Given an integer value, convert it into roman number.

prodevelopertutorial July 19, 2018

Problem Explanation:

Roman numbers are represented by below characters:

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

By using the above symbols we can further extend to deduce below numbers.

IV                                4
IX                                9
XL                               40
XC                              100
CD                              400
CM                              900

Example:

Input: 1234

Output: "MCCXXXIV"

Problem Solution:

This problem can be solved in many ways. The simplest solution is that is there below. We shall check with the highest base, take the corresponding Roman sign and subtract that base from the number. We shall continue the same steps until we convert the whole digit into decimal.

For example, if we need to convert the number 1234 into roman below are the steps:

Pass 1:
	Number 1234 > 1000, hence we take 1000 as base.
	Hence, 1234 – 1000, 1000 is “M” hence shall be written in “Roman Array”
	Roman Array = M
	Number = 234
Pass 2:
	Number 234 > 100, hence we take 100 as base.
	Hence, 234 – 100, 100 is “C” hence shall be written in “Roman Array”
	Roman Array = MC
	Number = 134
Pass 3:
	Number 134 > 100, hence we take 100 as base.
	Hence, 134 – 100, 100 is “C” hence shall be written in “Roman Array”
	Roman Array = MCC
	Number = 34
Pass 4:
	Number 34 > 10, hence we take 10 as base.
	Hence, 34 – 10, 10 is “X” hence shall be written in “Roman Array”
	Roman Array = MCCX
	Number = 24
Pass 5:
	Number 24 > 10, hence we take 10 as base.
	Hence, 24 – 10, 10 is “X” hence shall be written in “Roman Array”
	Roman Array = MCCXX
	Number = 14
Pass 6:
	Number 14 > 10, hence we take 10 as base.
	Hence, 14 – 10, 10 is “X” hence shall be written in “Roman Array”
	Roman Array = MCCXXX
	Number = 4
Pass 7:
	Number 4, we have saved a static value.
	Hence, 4 is “IV” hence shall be written in “Roman Array”
	Roman Array = MCCXXXIV

The solution in C:

#include <stdio.h>

  int main () 
  {
        int num;
        printf("Enter the number:");
        scanf("%d", &num);
        printf("Roman Number is: ");
        while (num > 0) 
        {
                if (num >= 1000) 
                {
                        /* M - 1000 */
                        printf("M");
                        num = num - 1000;
                } 
                else if (num >= 500) 
                {
                        /*
                         * D is 500. CM is 900
                         * CM = M - C = 1000 - 100 => 900
                         */
                        if (num >= 900) 
                        {
                                printf("CM");
                                num = num - 900;
                        } 
                        else 
                        {
                                printf("D");
                                num = num - 500;
                        }
                } 

                else if (num >= 100) 
                {
                        /*
                         * C is 100. CD is 400
                         * CD = D - C = 500 - 100 => 400
                         */
                        if (num >= 400) 
                        {
                                printf("CD");
                                num = num - 400;
                        } 
                        else 
                        {
                                printf("C");
                                num = num - 100;
                        }
                } 
                else if (num >= 50) 
                {
                        /*
                         * L is 50. XC is 90
                         * XC = C - X = 100 - 10 => 90
                         */
                        if (num >= 90) 
                        {
                                printf("XC");
                                num = num - 90;
                        } 
                        else 
                        {
                                printf("L");
                                num = num - 50;
                        }
                } 
                else if (num >= 9) 
                {
                        /*
                         * XL is 40. IX is 9. X is 10
                         * XL = L - X = 50 - 10 = 40
                         * IX = X - I = 10 - 1 = 9
                         */
                        if (num >= 40) 
                        {
                                printf("XL");
                                num = num - 40;
                        } 
                        else if (num == 9) 
                        {
                                printf("IX");
                                num = num - 9;
                        } 
                        else 
                        {
                                printf("X");
                                num = num - 10;
                        }
                } 
                else if (num >= 4) 
                {
                        /*
                         * V is 5 and IV is 4
                         * IV = V - I = 5 - 1 => 4
                         */
                        if (num >= 5) 
                        {
                                printf("V");
                                num = num - 5;
                        } 
                        else 
                        {
                                printf("IV");
                                num = num - 4;
                        }
                } 
                else 
                {
                        printf("I");
                        num = num - 1;
                }
        }
        printf("\n");
  }

Output:

Enter the number:1234
Roman Number is: MCCXXXIV

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Daily we discuss about competitive programming questions, join us at:   Telegram Channel

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2021 ProDeveloperTutorial.com
Get top courses from: Educative.io