Program to Converts between Roman Numerals and Arabic Numbers:

Download RomanNum - Converts between Roman Numerals and Arabic Numbers, QuickBASIC and C++ Versions http:http://www.oocities.org/hjsmithh/download.html#RomanNum## Generates Arabic Numbers when user types in Roman Numerals Generates Roman Numerals when user types in Arabic Numbers Establish Global variables Define rn = a string of characters for Roman numeral input Define rw = a string of characters for Roman numeral working storage Define an = an integer for the Arabic number computed Start- Ask user to "Enter a Roman Numeral or an Arabic Number (Q to exit)" Get number to convert from user as a string of characters rn Convert rn to all uppercase characters If rn contains a "Q" character then terminate the program If the first character of rn >= "0" and <= "9" then Set an = the Arabic number given in rn Output "The Roman Numeral for " an "is " Call the subroutine AToR to convert an to rw Output rw Else Set rw = rn Set an = 0 While rw contains the substring "CM" Replace the found substring in rw with blanks Set an = an + 900 While rw contains the substring "CD" Replace the found substring in rw with blanks Set an = an + 400 While rw contains the substring "XC" Replace the found substring in rw with blanks Set an = an + 90 While rw contains the substring "XL" Replace the found substring in rw with blanks Set an = an + 40 While rw contains the substring "IX" Replace the found substring in rw with blanks Set an = an + 9 While rw contains the substring "IV" Replace the found substring in rw with blanks Set an = an + 4 While rw contains the substring "M" Replace the found substring in rw with blanks Set an = an + 1000 While rw contains the substring "D" Replace the found substring in rw with blanks Set an = an + 500 While rw contains the substring "C" Replace the found substring in rw with blanks Set an = an + 100 While rw contains the substring "L" Replace the found substring in rw with blanks Set an = an + 50 While rw contains the substring "X" Replace the found substring in rw with blanks Set an = an + 10 While rw contains the substring "V" Replace the found substring in rw with blanks Set an = an + 5 While rw contains the substring "I" Replace the found substring in rw with blanks Set an = an + 1 Output "The Arabic Number for " rn " is " an Call the subroutine AToR to convert an to rw If rw equals rn then Output "The Roman Numeral is in the correct format" Else Output "The correct format for this is " rw Start over for next input Subroutine AToR Converts from Arabic Number an to Roman Numeral rw Set rw = "" the zero length string WHILE an >= 1000 Set rw = rw + "M" (add "M" to right end of rw) Set an = an - 1000 If an >= 900 then Set rw = rw + "CM", Set an = an - 900 [0, 999] If an >= 500 then Set rw = rw + "D", Set an = an - 500 [0, 899] If an >= 400 then Set rw = rw + "CD", Set an = an - 400 [0, 499] WHILE an >= 100 [0, 399] Set rw = rw + "C" Set an = an - 100 If an >= 90 then Set rw = rw + "XC", Set an = an - 90 [0, 99] If an >= 50 then Set rw = rw + "L", Set an = an - 50 [0, 89] If an >= 40 then Set rw = rw + "XL", Set an = an - 40 [0, 49] WHILE an >= 10 [0, 39] Set rw = rw + "X" Set an = an - 10 If an >= 9 then Set rw = rw + "IX", Set an = an - 9 [0, 9] If an >= 5 then Set rw = rw + "V", Set an = an - 5 [0, 8] If an >= 4 then Set rw = rw + "IV", Set an = an - 4 [0, 4] WHILE an >= 1 [0, 3] Set rw = rw + "I" Set an = an - 1 END of Subroutine AToR Sample output: Enter a Roman Numeral or an Arabic Number (Q to exit) 1995 The Roman Numeral for 1995 is MCMXCV Enter a Roman Numeral or an Arabic Number (Q to exit) MCMXCV The Arabic Number for MCMXCV is 1995 The Roman Numeral is in the correct format Enter a Roman Numeral or an Arabic Number (Q to exit) xccmmv The Arabic Number for XCCMMV is 1995 The correct format for this is MCMXCV Enter a Roman Numeral or an Arabic Number (Q to exit) q

Return to Number Theory, Algorithms, and Real Functions

Return to Harry's Home Page

This page accessed times since April 6, 2005.

Page created by: hjsmithh@sbcglobal.net

Changes last made on Saturday, 25-Apr-09 07:22:15 PDT