Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to convert a Roman number to an integer in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

var romanMap = map[rune]int{
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000,
}

func RomanToInt(s string) int {
    total := 0
    prevValue := 0

    // iterate backwards over the string
    for i := len(s) - 1; i >= 0; i-- {
        currentValue := romanMap[rune(s[i])]
        if currentValue < prevValue {
            total -= currentValue
        } else {
            total += currentValue
        }
        prevValue = currentValue
    }

    return total
}

func main() {
    roman := "XCVII"
    result := RomanToInt(roman)
    
    fmt.Printf("The integer value of %s is %d\n", roman, result)
}


/*
XCVII =
XC+V+I+I =
90+5+1+1 =
97
*/



/*
run:

The integer value of XCVII is 97

*/

 



answered Dec 3, 2025 by avibootz
...