Converting Numbers into Roman numerals

  • Context: Comp Sci 
  • Thread starter Thread starter Physics guy
  • Start date Start date
  • Tags Tags
    Numbers
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 1K views
Physics guy
Messages
23
Reaction score
10
Homework Statement
Write a program in python to convert number entered by a user to roman numerals
Relevant Equations
..
I have tried to solve this question for a while but I am not able to get the logic right.
 
Physics news on Phys.org
Show us what you have and use the code dialog box ( see < / > in the post editor menu next to the smiley face.

This is a common CS problem for introductory classes.

Have you tried the simpler approach of generating the numbers:

I, II, III, IIII, V, VI, VII, VIII, VIIII, X ...

and then looking for the ?IIII pattern and replacing it with the I(?+1) so that IIII is 0IIII which then transforms into IV and VIIII transforms into IX. Actually its a bit more general as you need to find any four repetitions and replace.

As an example, 1980 would be M-DCCCC-LXXX but DCCCC is written as CM and so the answer is M-CM-LXXX.

Using the simpler approach, you can then use modulo arithmetic to break a number down into its 1000*a + 500*b + 100*c + 50*d + 10*e + 5*f + g and then translate that to M*a + D*b + C*c + L*d + X*e + V*f + I*g where a,b,c,... are the number of times to repeat the roman numeral symbol.

I'm sure there are other more efficient approaches that can be found online.