/Design a program that supplies numbers (9000 only) and outputs their equivalent Roman numerals.
//the supply numbers and their equivalent Roman numerals are given below.

//Supply number equivalent Roman Numeral
//1 I
//5 V
//10 X
//50 L
//100 C
//500 D
//1000 M

Sample Output:
Enter integer (1-9000):1984
MCMLXXXIV
can anyone make a code for this problem.? using switch. i dont know where to start :(

## Recommended Answers

can anyone make a code for this problem.?

Yes, yes we can. But we won't because that defeats the purpose of the exercise which is for you to learn how to do it. If you have problems with your code, feel free to ask, but don't ask someone …

Loop through the value and find the largest possible numeral that will fit. Then print the numeral and decrement the value. For example:

``````while (value > 0) {
if (value >= 1000) {
// M
}
else if (value >= 500) {
// D
}
else if …``````

## All 8 Replies

can anyone make a code for this problem.?

Yes, yes we can. But we won't because that defeats the purpose of the exercise which is for you to learn how to do it. If you have problems with your code, feel free to ask, but don't ask someone to write all of the code for you.

well can u give tips on what formula should i use.?

Loop through the value and find the largest possible numeral that will fit. Then print the numeral and decrement the value. For example:

``````while (value > 0) {
if (value >= 1000) {
// M
}
else if (value >= 500) {
// D
}
else if (value >= 100) {
// C
}
...
}
``````

This approach isn't well suited to a switch statement as a switch is basically a table lookup with exact values and you really care about the range. But you can voodoo something workable with a little creativity. A start might be to extract your keys based on the value, then check them:

``````while (value > 0) {
const keys[] = {1000, 500, 100, 50, 10, 5, 1};
int i;

for (i = 0; i < sizeof keys / sizeof *keys; i++) {
if (value >= keys[i]) {
break;
}
}

switch (keys[i])
{
case 1000:
// M
break;
case 500:
// D
break;
case 100:
// C
break;
...
}
}
`````` For the love god give the man the code but also show him how to do it

commented: Please go read our rules. He's shown no proof of effort yet. -3

No he will not. We help but not solve the problem. Ask your friend to provide what he has done so far.

i just started learning switch statement. and they gave me that problem. OMG :( what should i do? if anyone can give me their code on that prob, i will study it. ill try to make my code and post it here also:|

Here is the 10 penny description.

A switch statement allows you to execute code based upon the value of an integer (or comparable - unsigned, long, etc is ok). Let's say that 0 == off, 1 == on, 2 == on+bright, and anything else is an error (or does nothing). Following me so far? Then look at this:

``````for (int setting = 0; i < 10; i++)
{
switch (setting)
{
case 0: // Turn off light.
break;
case 1: // Turn on light.
break;
case 2: // Make light bright.
break;
default:
// Not a valid value.
break;
}
sleep(10);
}
``````

Assuming you had the extra code to control the lights, this would result in no light, 10 seconds later the light would turn on, and 10 seconds after that the light would be set to a bright setting. Anything else would not change the light setting (the default case). Also note the "break;" statements. Those keep your code from "falling through" to the next case, effectively breaking out of the switch block.

``````    this what i've done so far :) thanks for all your response.

#include <iostream>

using namespace std;

int main()
{
int number, M,C,X,I,Mr,Cr,Xr,Ir;
char YN=('y','Y');

cout << "\n";

do {
cout << "Enter Integer: ";
cin >> number;

M=number/1000;
Mr=number%1000;
C=Mr/100;
Cr=Mr%100;
X=Cr/10;
Xr=Cr%10;
I=Xr/1;
Ir=Xr%1;

cout << "\n";

switch (M){
case 1:
cout << "M";
break;
case 2:
cout << "MM";
break;
case 3:
cout << "MMM";
break;
case 4:
cout << "MMMM";
break;
case 5:
cout << "MMMMM";
break;
case 6:
cout << "MMMMMM";
break;
case 7:
cout << "MMMMMMM";
break;
case 8:
cout << "MMMMMMMM";
break;
case 9:
cout << "MMMMMMMMM";
break;
default:
cout << "";
}

switch (C){
case 1:
cout << "C";
break;
case 2:
cout << "CC";
break;
case 3:
cout << "CCC";
break;
case 4:
cout << "CD";
break;
case 5:
cout << "D";
break;
case 6:
cout << "DC";
break;
case 7:
cout << "DCC";
break;
case 8:
cout << "DCCC";
break;
case 9:
cout << "CM";
break;
default:
cout << "";
}
switch (X){
case 1:
cout << "X";
break;
case 2:
cout << "XX";
break;
case 3:
cout << "XXX";
break;
case 4:
cout << "XC";
break;
case 5:
cout << "L";
break;
case 6:
cout << "LX";
break;
case 7:
cout << "LXX";
break;
case 8:
cout << "LXXX";
break;
case 9:
cout << "XC";
break;
default:
cout << "";
}
switch (I){
case 1:
cout << "I\n";
break;
case 2:
cout << "II\n";
break;
case 3:
cout << "III\n";
break;
case 4:
cout << "IV\n";
break;
case 5:
cout << "V\n";
break;
case 6:
cout << "VI\n";
break;
case 7:
cout << "VII\n";
break;
case 8:
cout << "VIII\n";
break;
case 9:
cout << "IX\n";
break;
default:
cout << "\n";
}

cout << "\n";
cout << "Try again? (Y/N) ";
cin >> YN;

} while (YN=='Y'||YN=='y');
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.