Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am having a problem with a program that converts numbers to roman numerals. I used a do-while function but i'm thinking about using a swith instead. I'm fairly new to programming and still finding things kind of difficult. Here is what I have so far and I know it isn't correct and probally wont work, it's just what I have so far...
#include
main()
{
int the_number
char roman_numeralprintf( "Enter an integer value from 1 to 3000:" );
scanf( "%i", &the_number );while ( the_number => 1; the_number 1 )
printf( "M" );
for ( the_number / 900; the_number => 1 )
printf( "CM" );
for ( the_number / 500; the_number => 1 )
printf( "D" );
}
}Here is some steps to convert and integer value to its roman numeral equivalent....
1. While integer value divided by 1000 is greater than or equal to 1
A. Print roman numeral "M"
B. Subtract 1000 from the integer value
2. While integer value divided by 900 is greater than or equal to 1
A. Print roman numeral "CM"
B. Subtract 900 from the integer value
3. While integer value divided by 500 is greater than or equal to 1
A. Print roman numeral "D"
B. Subtract 500 from the integer value
4. While integer value divided by 400 is greater than or equal to 1
A. Print roman numeral "CD"
B. Subtract 400 from the integer value
5. While integer value divided by 100 is greater than or equal to 1
A. Print roman numeral "C"
B. Subtract 100 from the integer value
6. While integer value divided by 90 is greater than or equal to 1
A. Print roman numeral "XC"
B. Subtract 90 from the integer value
7. While integer value divided by 50 is greater than or equal to 1
A. Print roman numeral "L"
B. Subtract 50 from the integer value
8. While integer value divided by 40 is greater than or equal to 1
A. Print roman numeral "XL"
B. Subtract 40 from the integer value
9. While integer value divided by 10 is greater than or equal to 1
A. Print roman numeral "X"
B. Subtract 10 from the integer value
10. While integer value divided by 9 is greater than or equal to 1
A. Print roman numeral "IX"
B. Subtract 9 from the integer value
11. While integer value divided by 5 is greater than or equal to 1
A. Print roman numeral "V"
B. Subtract 5 from the integer value
12. While integer value divided by 4 is greater than or equal to 1
A. Print roman numeral "IV"
B. Subtract 4 from the integer value
13. While integer value divided by 1 is greater than or equal to 1
A. Print roman numeral "I"
B. Subtract 1 from the integer valueThe program suppose to print out like this...
The Roman numeral equivalent of 12 is XII.
The Roman numeral equivalent of 76 is LXXVI
so on and so on....It is easy to understand but just hard to put into programming words for me in c. If someone could help me out or even post the script for it I would very much appriciate it. Thanks!

You won't learn much if someone posts the code for you.
A couple of thoughts:
Note that each of the numbered instructions are sequential, not repeating. The instructions don't say, do the #1 stuff, then do the #2 stuff, then the #3 stuff, then go back to #1. That's basically what your code is doing.
I didn't look thru every step of the algorithm, but at least for the beginning, each of the numbered steps should be a stand alone while loop. For example, step 1 would be:
while ((the_number / 1000) > 1) {
printf("M");
the_number -= 1000;
}The step 2 code will be a separate loop the follows the above loop. It's not inside the above loop.
In your code, you didn't do the B part of each step, which will cause your loops to never end.

It's a lot simpler to lose the divides. Instead of saying:
while integer value divided by 1000 is greater than or equal to one,
why don't you just say:
while the integer value is greater than or equal to 1000?
Some of them are guaranteed to never loop more than once, in which case you could use if statements instead of whiles. For example, by the time you get to 900, you already know it can't be more than 1000.
while (the_number >= 1000)
{
printf("M");
the_number -= 1000;
}
if (the_number >= 900)
{
printf("CM");
the_number -= 900;
}
if (the_number >= 500)
{
printf("D");
the_number -= 500;
}
...This kind of program doesn't really lend itself well to switch statements, because they have to be == operators, and you're using a lot of >= operators.
If you really want to make it smaller, you could use a couple of arrays, and only use one while statement inside a for loop.
int BaseTen[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
char (*Roman)[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};Then your for loop could be like this:
for (i = 0; i < 13; ++i)
while (the_number >= BaseTen[i])
{
printf("%s", Roman[i]);
the_number -= BaseTen[i];
}

Ok.. Here is what I came up from the advice given so far... When I try to run it, it gives me several errors so if someone can offer more help it would be great...
#include
main()
{
int the_numberprintf( "Enter an integer value from 1 to 3000:" );
scanf( "%i", &the_number );while ( the_number >= 1; the_number = 1000
{
printf( "M" );
the_number -= 1000;
}
if the_number >= 900
{
printf( "CM" );
the_number -= 900;
}
if the_number >= 500
{
printf( "D" );
the_number -= 500;
}
if the_number >= 400
{
printf( "CD" );
the_number -= 400;
}
if the_number >= 100
{
printf( "C" );
the_number -= 100;
}
if the_number > 90
{
printf( "XC" );
the_number -= 90;
}
if the_number > 50
{
printf( "L" );
the_number -= 50;
}
if the_number > 40
{
printf( "XL" );
the_number -= 40;
}
if the_number > 10
{
printf( "X" );
the_number -= 10;
}
if the_number > 9
{
printf( "IX" ):
the_number -= 9;
}
if the_number > 5
{
printf( "V" );
the_number -= 5;
}
if the_number > 4
{
printf( "IV" );
the_number -= 4;
}
if the_number > 1
{
printf( "I" );
the_number -= 1;
}
}

![]() |
Help with the toolbar
|
How to get image to show ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |