# Thread: Convert the number to Roman numerals

1. Member
Join Date
Jan 2010
Posts
80
Rep Power
0

## Convert the number to Roman numerals

Hi

I have been asked to write code that converts the number to Roman numerals.

Fortunately, I wrote the code and it works properly.

My question is

In standard Roman numerals, no more than three consecutive copies of the same letter are used.

I have tried many times to discover what the proper statement which I must write in my code is that voids using three consecutive copies of the same letter

this is the code

Java Code:
```import java.util.Scanner;

public class RomanNumeral {
public static void main(String[] args){

Scanner scan = new Scanner(System.in);
System.out.print("Please enter a number between 1 and 3999: ");
int number = scan.nextInt();

String roman="";

while(number<=0 || number>3999){
System.out.println("Invalid input.  You must enter a number between 1 and 3999");
System.out.print("Please enter another number now: ");
number = scan.nextInt();
}

while(number>=1000){
roman += "M";
number-=1000;
}

while(number>=900){
roman += "CM";
number-=900;
}

while(number>=500){
roman += "D";
number-=500;
}

while(number>=400){
roman += "CD";
number-=400;
}

while(number>=100){
roman += "C";
number-=100;
}

while(number>=90){
roman += "XC";
number-=90;
}

while(number>=50){
roman += "L";
number-=50;
}

while(number>=40){
roman += "XL";
number-=40;
}

while(number>=10){
roman += "X";
number-=10;
}

while(number>=9){
roman += "IX";
number-=9;
}

while(number>=5){
roman += "V";
number-=5;
}

while(number>=4){
roman += "IV";
number-=4;
}

while(number>=1){
roman += "I";
number-=1;
}

System.out.println(roman);
}
}```
I hope you can help me

Thanks

:)

2. Originally Posted by ŖàΫ ỏƒ Ңόρę
Hi
Fortunately, I wrote the code and it works properly.

I have tried many times to discover what the proper statement which I must write in my code is that voids using three consecutive copies of the same letter
It's OK for 3 consecutive letters to occur, just not 4. Your code seems to me to work just fine, so I'm not sure what your question is.

Oh, and thanks for starting a new thread! :)

3. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
removed......
Last edited by al_Marshy_1981; 02-22-2010 at 12:18 AM. Reason: brain is turning to mush

4. Originally Posted by al_Marshy_1981
If the number entered was 1111, then the roman String would be: "IIII" which is wrong. I think that is what he means.
Huh? If I run this code and enter "1111" I get MCXI back which is the correct answer. Am I missing something here?

5. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
haha yes you are right I thought number was being reduced to zero for some reason in each while loop. Time for bed I think :D

6. Member
Join Date
Jan 2010
Posts
80
Rep Power
0
Thank u 4 response

I have another problem

The output should be the same format

Java Code:
```Please enter a number between 1 and 3999: 1073
0 in Roman numerals is MLXXIII```
Instead of 0 , the programe must gives the same number that the user has entered

I've tried to fix this problem but I couldn't

7. Originally Posted by ŖàΫ ỏƒ Ңόρę
Thank u 4 response
You're welcome, but what the heck happened to your first problem? Have you decided to just ignore it? We need closure here.

edit: hm, I guess you've been abducted by aliens...
Last edited by Fubarable; 02-23-2010 at 06:05 AM.

8. Member
Join Date
Jan 2010
Posts
80
Rep Power
0
Hi Fubarable

Thank you for your passing and responsing

I just was confused by this rule

In standard Roman numerals, no more than three consecutive copies of the same letter are used.

Also the second problem, I fixed yesterday

This is code after fixing

Java Code:
```import java.util.Scanner;

public class RomanNumeral {
public static void main(String[] args){

Scanner scan = new Scanner(System.in);
System.out.print("Please enter a number between 1 and 3999: ");
int number = scan.nextInt();
[COLOR="Red"]int number1 = number;[/COLOR] // I wrote this variable to hold the number that the user enters

String roman="";

while(number<=0 || number>3999){
System.out.println("Invalid input.  You must enter a number between 1 and 3999");
System.out.print("Please enter another number now: ");
number = scan.nextInt();
[COLOR="Red"]number1=number;[/COLOR]
}

while(number>=1000){
roman += "M";
number-=1000;
}

while(number>=900){
roman += "CM";
number-=900;
}

while(number>=500){
roman += "D";
number-=500;
}

while(number>=400){
roman += "CD";
number-=400;
}

while(number>=100){
roman += "C";
number-=100;
}

while(number>=90){
roman += "XC";
number-=90;
}

while(number>=50){
roman += "L";
number-=50;
}

while(number>=40){
roman += "XL";
number-=40;
}

while(number>=10){
roman += "X";
number-=10;
}

while(number>=9){
roman += "IX";
number-=9;
}

while(number>=5){
roman += "V";
number-=5;
}

while(number>=4){
roman += "IV";
number-=4;
}

while(number>=1){
roman += "I";
number-=1;
}

System.out.println(number1 + " in Roman numerals is " + roman);
}
}```
If there is another way to solve that please tell me.

Thanks

9. Member
Join Date
Nov 2012
Posts
1
Rep Power
0

## Re: Convert the number to Roman numerals

import java.util.Scanner;

public class Teste {

public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Type a number");
int num = -1;
String roman = "";
do{
if(sc.hasNextInt())
num = sc.nextInt();
else{
System.out.println("Error");
sc.nextLine();
sc.next();
}

}while(num<=0 && num>3999);

while(num>=900){
if(num==900){
roman+="CM";
num=0;
}
else{
roman+="M";
num-=1000;
}
}
while(num>=400){
if(num==400){
roman+="CD";
num=0;
}
else{
roman+="D";
num-=500;
}
}
while(num>=90){
if(num==90){
roman+="XC";
num=0;
}
else{
roman+="C";
num-=100;
}
}
while(num>=40){
if(num==40){
roman+="XL";
num=0;
}
else{
roman+="L";
num-=50;
}
}
while(num>=9){
if(num==9){
roman+="IX";
num=0;
}
else{
roman+="X";
num-=10;
}
}
while(num>=4){
if(num==4){
roman+="IIIV";
num=0;
}
else{
roman+="V";
num-=5;
}
}
while(num>=1){
roman+="I";
num-=1;
}
System.out.println("Msg " +roman);

}

}

10. Moderator
Join Date
Jul 2010
Location
California
Posts
1,642
Rep Power
6

## Re: Convert the number to Roman numerals

@Rocknsock, any reason you decided to resurrect a thread that is almost 3 years old to spoonfeed code? Recommended reading:
The Problem with Spoon-feeding

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•