www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Integer To Roman

Last update: 12/26/2016 9:54:00 PM

By Fatih KABAKCI

The problem is to convert an integer value which is defined between 1 and 3999, to roman value.

Problem #12 - Integer To Roman

Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Difficulty: Medium

Solution in Java

Reference Roman Table

http://www.mathatube.com/tables-charts-roman-numeral-chart-html.html

package com.fatihkabakci.Medium.IntegerToRoman;
/**
 * 
 * @author fkabakci
 * 
 * Problem Description: Given an integer, convert it to a roman numeral. 
 * Input is guaranteed to be within the range from 1 to 3999.
 * 
 * Solution:
 * 
 * Precondition: num is within the range from 1 to 3999
 * 
 * 1. Decrease num as its defined maximum roman value while it is higher than 0.
 */

public class IntegerToRoman {
	public String intToRoman(int num) {
		String x = new String();
		while(num > 0) {
			if(num >= 1000) {
				x += "M";
				num -= 1000;
			}else if(num >= 900) {
				x += "CM";
				num -= 900;
			}else if(num >= 500) {
				x += "D";
				num -= 500;
			}else if(num >= 400) {
				x += "CD";
				num -= 400;
			}else if(num >= 100) {
				x += "C";
				num -= 100;
			}else if(num >= 90) {
				x += "XC";
				num -= 90;
			}else if(num >= 50) {
				x += "L";
				num -= 50;
			}else if(num >= 40) {
				x += "XL";
				num -= 40;
			}else if(num >= 10) {
				x += "X";
				num -= 10;
			}else if(num >= 9) {
				x += "IX";
				num -= 9;
			}else if(num >= 5) {
				x += "V";
				num -= 5;
			}else if(num >= 4) {
				x += "IV";
				num -= 4;
			}else if(num >= 1) {
				x += "I";
				num -= 1;
			}
		}
		return x;
    }
	
	public static void main(String[] args) {
		IntegerToRoman itr = new IntegerToRoman();
		for(int i = 1; i < 3999; i++)
			System.out.println(itr.intToRoman(i));
	}
}

Shorter Solution in Java by the member Fabrizio3

public static String intToRoman(int num) {
    String M[] = {"", "M", "MM", "MMM"};
    String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}

Similar Problems

#13 Roman To Integer

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.