- #1
- 4,652
- 38
Hi,
I just began working on a little program to convert base 10 number to Roman numerals. I couldn't figure out how to make a loop to count how many Ms, Ds, Cs etc. so I wrote it out the long way for now.
I can see a lot a repetition and an obvious pattern in my divisors, since I am dividing by
103 , (103)/2 , 102 , (102)/2 ,etc.
but I can't figure out a loop that will handle this properly. Any suggestions?
(note: I haven't written the logic for the last piece of it yet which will deal with the order of the numbers, for instance, converting VIIII to IX.)
Thanks for any advice on handling the loop. Here's what I did:
I just began working on a little program to convert base 10 number to Roman numerals. I couldn't figure out how to make a loop to count how many Ms, Ds, Cs etc. so I wrote it out the long way for now.
I can see a lot a repetition and an obvious pattern in my divisors, since I am dividing by
103 , (103)/2 , 102 , (102)/2 ,etc.
but I can't figure out a loop that will handle this properly. Any suggestions?
(note: I haven't written the logic for the last piece of it yet which will deal with the order of the numbers, for instance, converting VIIII to IX.)
Thanks for any advice on handling the loop. Here's what I did:
Code:
# include <iostream>
using namespace std;
//converts base 10 to romans
int main()
{
int num;
int quotient;
int M = 0;
int D = 0;
int C = 0;
int L = 0;
int X = 0;
int V = 0;
int I = 0;
cin >> num;
quotient = num/1000;
M = quotient;
num = num % 1000;
quotient = num/500;
D = quotient;
num = num % 500;
quotient = num/100;
C = quotient;
num = num % 100;
quotient = num/50;
L = quotient;
num = num % 50;
quotient = num/10;
X = quotient;
num = num % 10;
quotient = num/5;
V = quotient;
num = num % 5;
I = num;
for (int m = 0; m < M; m++)
cout << "M";
for (int d = 0; d < D; d++)
cout << "D";
for (int c = 0; c < C; c++)
cout << "C";
for (int l = 0; l < L; l++)
cout << "L";
for (int x = 0; x < X; x++)
cout << "X";
for (int v = 0; v < V; v++)
cout << "V";
for (int i = 0; i < I; i++)
cout << "I";
return 0;
}