python programming
Question #4: # Years ago the Romans used a different system to represent numbers. # Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans # formed numbers by joining combinations of the characters # (I, V, X, L, C, D, and M). # Roman Numeral characters and their integer values are: # I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000. # Examples of simple Roman Numerals are: # VIII (5+1+1+1 = 8), XV (10+5 = 15), LXVI (50+10+5+1 = 66), # CLXXV (100+50+10+10+5 = 175), MXV (1000+10+5 = 1015), # where the integer amounts are arrived at by simply adding the # individual numeral values one after the other. # However, the Romans decided not to use the same 4 consecutive numerals # when forming numbers, and instead formed values such as 4, 9, 14, 19, # etc., using subtraction as follows: # IV (5-1 = 4), IX (10-1 = 9), XIV (10+5-1 = 14), # where a smaller numeral is placed to the left of the larger numeral, # and the smaller numerals value is subtracted from the larger numerals # value. # As well, only the numerals I, X, and C are used for subtraction, # and then, only in the following cases: # I can only be subtracted from V or X, # X can only be subtracted from L or C, and # C can only be subtracted from D or M. # Additionally, at most a single (1) numeral can be subtracted # from another numeral. # So, with the symbols and rules above, write the code for a Python # function that accepts an integer number from 1 to 3999 and displays # the value as a Roman Numeral (using the following dictionary): # keys: I, V, X, L, C, D, M # values: 1, 5, 10, 50, 100, 500, 1000 # For example: # Enter a value from 1 to 3999: 1929 # 1929 = MCMXXIX # index 0 1 2 3 4 5 6 # _ _ _ _ _ _ _ # |M|C|M|X|X|I|X| # | |_| | | |_| # 1000__| | | | |____9 1000 + 900 + 10 + 10 + 9 = 1929 # | | |10 # 900______| | # |_10 # HINT: The only situations in which you will need to consider subtraction # is when the ones, tens, or hundreds columns are either 4 or 9. # Start by iterating through the Roman numeral from left to right. # Copy the integer value to a temporary variable and using a while loop # continue traversing as long as the variable is > than 0. # Begin by determining if the number / 1000 results in a value >= 1, and # if so, given that the first digit can only be a 1, 2, or 3, the Roman # numeral will begin with either an "M", "MM", or "MMM" (added to a string). # After determining the the first digit's value, subtract that amount # (either 1000, 2000, or 3000) from the number. # Then, determine the value of the hundred's digit by dividing by 100. # If the digit is a 9, 5, or 4, then the hundreds portion will result in a # Roman numeral of "CM", "D", or "CD" (added to the string), and requiring # subtraction of either 900, 500, or 400 from the number. If the hundred's # digit is any other value, simply add a "C" to the string and subtract 100 # each time through the loop. # # Continue this process for the 10's and 1's columnns using the appropriate # symbols for those digits! # NOTE: This solution may require many if : / elif : / else : statements! # # Sample main program num = int(input("Enter a value from 1 to 3999: ")) for i in range(1, num + 1) : intToRoman(i) input( ) # pause the output until ENTER key is pressed
def intToRoman(num):
value=[1,4,5,9,10,40,50,90,100,400,500,900,1000]
represent=['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M']
roman_number="
iterator=0
while num>0:
for _ in range(num//value[i]):
roman_number+=represent[i]
num-=value[i]
iterator+=1
return roman_number
num=int(input("Enter a value from 1 to 3999: "))
for i in range(1,num+1):
res=intToRoman(i)
print(res)
sign=input().lower()
if sign=='enter':
break
here is the code to covert integer value to roman number we used two list for those values which are mostly used and referred them to make the remaining numbers. This is the easy procedure to solve this question
Get Answers For Free
Most questions answered within 1 hours.