0% found this document useful (0 votes)
2 views1 page

Java 1

This Java program converts an integer input into its Roman numeral representation. It uses a HashMap to map specific integer values to their corresponding Roman numeral symbols and iteratively subtracts from the input number while appending the symbols to a StringBuilder. The final Roman numeral is then printed to the console.

Uploaded by

JUREG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Java 1

This Java program converts an integer input into its Roman numeral representation. It uses a HashMap to map specific integer values to their corresponding Roman numeral symbols and iteratively subtracts from the input number while appending the symbols to a StringBuilder. The final Roman numeral is then printed to the console.

Uploaded by

JUREG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package main;

import java.util.Scanner;
import java.util.HashMap;

public class Program {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);


StringBuilder sb = new StringBuilder();

System.out.print("Digite o numero: ");


int n = sc.nextInt();

int[] list = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};

HashMap<Integer, String> tabelaHash = new HashMap<>();

tabelaHash.put(1, "I");
tabelaHash.put(4, "IV");
tabelaHash.put(5, "V");
tabelaHash.put(9, "IX");
tabelaHash.put(10, "X");
tabelaHash.put(40, "XL");
tabelaHash.put(50, "L");
tabelaHash.put(90, "XC");
tabelaHash.put(100, "C");
tabelaHash.put(400, "CD");
tabelaHash.put(500, "D");
tabelaHash.put(900, "CM");
tabelaHash.put(1000, "M");

while(n != 0){
for(int i = 1; i < list.length; i++) {
if(n < list[i]) {
sb.append(tabelaHash.get(list[i-1]));
n -= list[i-1];
break;
}
}
}

System.out.printf("Resposta em romano: %s", sb.toString());

sc.close();

You might also like