Converting Integers to Roman Numbers

In this article, we will teach you how to convert an integer into a roman number in java, python, and c++.

The objective of this program is for users to enter the integer value in the console and the roman number show as an output of the screen. I think now you will know what we have to do in this program.

Before doing coding look at some steps to do it.

step 1: Take user input as an integer value.

step2: Call the function convert_integer_to_roman and give user input as an argument.

step3: In function, we define some variables like a result which holds our output, roman_number which holds some pre-defined roman number, and an integer which holds our roman_number corresponding integer values.

step 4: n-integer[i]>=0, check this condition in while loop and this while loop run in the for a loop. if the condition was true then add result with roman_number[i] and subtract n with integer[i] else condition false then out from while loop.

step5: At last, we return our result.

Now if you do not know what we're doing above so don't worry follow the chart.


Now let ready to do the program 

In java,

 class Roman{
    static String convert_number_to_roman(int n){
        String result="";
        String roman_number[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int integer[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        for(int i=0;i<13;i++){
            while(n-integer[i]>=0){
                result+=roman_number[i];
                n-=integer[i];
            }
        }
        return result;
    }
    public static void main(String[] args) {
        System.out.print("Conversion is :: "+convert_integer_to_roman(7)+"\n");
    }
}
Output:
  Conversion is :: VII

In python,
def convert_integer_to_roman(n):
    roman_number=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
    integer=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
    result=""
    for i in range(0,13):
        while(n-integer[i]>=0):
            result+=roman_number[i]
            n-=integer[i]
    return result
n=int(input("\nEnter the integer value "))
print("Conversion is :: "+convert_integer_to_roman(n))
Output:
Enter the integer value 77
Conversion is :: LXXVII

In c++,
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
string convert_number_to_roman(int n){
    string result="";
    string roman_number[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
    int integer[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
    for(int i=0;i<13;i++){
        while(n-integer[i]>=0){
            result+=roman_number[i];
            n-=integer[i];
        }
    }
    return result;
}
int main(){
    int n;
    cout<<"\nEnter the integer value :: ";
    cin>>n;
    cout<<"\nConversion is :: "<<convert_number_to_roman(n);
    return 0;
}
output:
Enter the integer value 77
Conversion is :: LXXVII

//------------------------------Bonus for You------------------------------//

How to become a successful YouTuber?

If you want to become a successful youtube in your life so you have to know everything about youtube like how SEO works, how to rank your video on youtube, how to get more traffic, and so on. But for beginners, it is complicated to rank your video on youtube, to get traffic on your youtube channel, and more. So don't worry about it. if you want to become a YouTuber we can help you. Click here and buy the youtube manifest program at an affordable price. This program helps you to become a successful YouTuber and the result of this program was very good.



Post a Comment

0 Comments