Coding Challenge Day 20- Problem 1: Convert a given number to it’s Roman numeral equivalent

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 20: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Convert a given number to it’s Roman numeral equivalent

SYMBOL       VALUE
I             1
IV            4
V             5
IX            9
X             10
XL            40
L             50
XC            90
C             100
CD            400
D             500
CM            900 
M             1000   

Sample input: number=50
Output: Roman numeral: L

Sample input: number=534
Output: Roman numeral: DXXXIV

Solution:

I learnt to solve this one from this LINK

package misc;

public class Program004roman_numeral_converter {
	
	public static int digit (char ch, int num, int i, char c[]){
		for (int j=0; j<num; j++)
		c[i++]=ch;
		return i;
	}
	
	public static int sub_digit (char ch1, char ch2, int i, char c[]){
		c[i++]= ch1;
		c[i++]= ch2;
		
		return i;
	}
	public static void roman_numeral (int number){
		
		char [] c= new char[100];
		int i=0;
		
		if(number <=0){
			System.out.println("Number less than 0");
			return;
		}
		
		while(number!=0){
			if(number>=1000){
				i=digit ('M', number/1000, i,c);
				number=number%1000;
			}
			
			else if (number>=500){
				
				if(number<900){
					i=digit ('D', number/500, i, c);
					number%=500;
				}
				
				else {
					i= sub_digit ('C', 'M', i, c);
					number %= 100;
				}
			}
			
			else if (number >=100){
				
				if(number<400){
					i= digit ('C', number/100, i, c);
					number %= 100;
				}
				
				else {
					
					i= sub_digit ('C', 'D', i, c);
						number %= 100;
				}
			}
				
				else if (number >=50){
					
					if (number <90){
						i= digit ('L', number/50, i, c);
						number%=10;
					}
					
					else {
						i=sub_digit ('X', 'C', i, c);
						number %=10;
					}
				}
				
				else if (number >= 10){
					
					if (number < 40){
						i= digit ('X', number/10, i, c);
						number %=10;
					}
					
					else {
						i= sub_digit ('X', 'L', i, c);
						number%= 10;
					}
				}
				
				else if (number >= 5){
					if (number <9){
						i= digit ('V', number/5, i, c);
						number %= 5;
					}
					
					else {
						i= sub_digit ('I', 'X', i, c);
						number=0;
					}
				}
				
				else if (number >= 1){
					if (number <4){
						i=digit ('I', number, i, c);
						number=0;
					}
					
					else {
						i= sub_digit ('I', 'V', i ,c);
						number=0;
					}
				}
			}
			
			System.out.print("Roman numeral: ");
			for (int j=0; j<i; j++){
				System.out.printf("%c", c[j]);
			}
		}
		
	public static void main (String [] args){
		int number=50;
		
		roman_numeral(number);
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close