# Convert the number to Roman numerals

• 02-21-2010, 11:15 PM
ŖàΫ ỏƒ Ңόρę
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

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

:)
• 02-21-2010, 11:22 PM
Fubarable
Quote:

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! :)
• 02-22-2010, 12:04 AM
al_Marshy_1981
removed......
• 02-22-2010, 12:11 AM
Fubarable
Quote:

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?
• 02-22-2010, 12:17 AM
al_Marshy_1981
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
• 02-23-2010, 05:26 AM
ŖàΫ ỏƒ Ңόρę
Thank u 4 response

I have another problem

The output should be the same format

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
• 02-23-2010, 05:29 AM
Fubarable
Quote:

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...
• 02-23-2010, 03:57 PM
ŖàΫ ỏƒ Ңόρę
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

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
• 11-29-2012, 11:43 PM
Rocknsock
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);

}

}
• 11-29-2012, 11:44 PM
doWhile
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