Go Language Program to Convert Decimal to Roman Numerals


Roman numerals are an ancient numerical system that has found its way into modern applications like clock faces, book chapters, and movie credits. In this article, we are going to Convert Decimal to Roman Numerals. We will look at two different examples, in the first example we will use the recursive approach and in the second example we are going to use the iterative approach.

Explanation

Roman numerals have been one of the languages to express the marvel of Mathematics. Clock faces, book chapters, and movie credits are just some of the current uses for the old Roman number system. Converting from decimal to Roman numerals is often quite necessary.

Here are few roman numeral symbol and their corresponding decimal:

I: 1

IV: 4

V: 5

IX: 9

X: 10

XL: 40

L: 50

XC: 90

C: 100

CD: 400

D: 500

CM: 900

M: 1000

Syntax

func decimalToRomanRecursive(num int) string

The syntax defines function called decimalToRomanRecursive which converts an input integer recursively to its corresponding Roman numeral representation, using the predefined romanMap.

Algorithm

  • Start an outer loop from 1 to the desired number of rows.

  • Start an inner loop from 1 to the current row number (rowCount).

  • Print the value of num and increment it by 1.

  • After the inner loop ends, increment the rowCount by 1.

  • Repeat steps 2 to 4 until the outer loop ends.

Example 1

In this example, we will Convert Decimal to Roman Numerals, we illustrate the process by which a chart of decimal numbers and their equivalent Roman numerals may be constructed. We then add Roman numeral sign for largest decimal value less than or equal to provided integer. The next step is to subtract decimal value from original number. This process is repeated until the number reaches zero.

package main
import (
	"fmt"
)
var romanMap = []struct {
	decVal int
	symbol string
}{
    {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
	{100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
	{10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"},
}
func decimalToRomanRecursive(num int) string {
	if num == 0 {
    	return ""
	}
	for _, pair := range romanMap {
    	if num >= pair.decVal {
            return pair.symbol + decimalToRomanRecursive(num-pair.decVal)
        }
    }
	return ""
}
func main() {
	num := 354
    fmt.Printf("Decimal: %d\n", num)
    roman := decimalToRomanRecursive(num)
	fmt.Printf("Roman: %s\n", roman)
}

Output

Decimal: 354
Roman: CCCLIV

Example 2

In this example,Convert Decimal to Roman Numerals, we make a chart depicting the relationship between decimal places and the Roman numerals they represent. After an empty result string is created, we loop over the Roman numerals from highest to lowest. The largest decimal number is found using a for loop, and the appropriate Roman numeral is appended. As long as input value is more than or equal to current decimal value, result will be appended with Roman sign, and input will be subtracted from the value. Iterate until you get a result of zero.

package main
import "fmt"
var romanMap = []struct {
	decVal int
	symbol string
}{
	{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
	{100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
	{10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"},
}
func decimalToRomanIterative(num int) string {
	result := ""
	for _, pair := range romanMap {
     	for num >= pair.decVal {
         	result += pair.symbol
          	num -= pair.decVal
    	}
	}
	return result
}
func main() {
	num := 354
	fmt.Printf("Decimal: %d\n", num)
	roman := decimalToRomanIterative(num)
	fmt.Printf("Roman: %s\n", roman)
}

Output

Decimal: 354
Roman: CCCLIV

Real Life Implementation

  • Monuments and Building Cornerstones: Monuments and structures Roman numerals are often used on cornerstones, plaques, or inscriptions to indicate the year a building or monument was built or dedicated. This strategy honors previous customs while imbuing the structure with a timeless quality.

  • Movie Copyright Dates: In certain cases, the beginning of some films may include the display of the copyright year rendered in Roman numerals. Cinematic works that are meant to inspire nostalgia typically use this aesthetic approach, giving them a more traditional feel.

Conclusion

Mathematics has so many different forms of language, and Roman numerals are just one example. You can find it on clock faces, book chapters, and even in film titles. In this article, we have seen two different examples to Convert Decimal to Roman Numerals Go. The recursive approach is appropriate for low to medium input quantities due to its simplicity and intuitiveness, whereas while dealing with bigger decimal numbers, the iterative techniques provide greater performance and are thus preferred.

Updated on: 18-Oct-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements