Roman Numeral in Powershell

Published on 04 July 2025 (Updated: 04 July 2025)

Welcome to the Roman Numeral in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

$Romans = @{
    M = 1000
    D = 500
    C = 100
    L = 50
    X = 10
    V = 5
    I = 1
}

function Show-Usage() {
    Write-Host 'Usage: please provide a string of roman numerals'
    Exit 1
}

function Show-Error() {
    Write-Host 'Error: invalid string of roman numerals'
    Exit 1
}

function RomanNumeral([string]$Str) {
    $total = 0
    $prevValue = 0
    foreach ($ch in $Str.ToCharArray()) {
        $value = $Romans[[string]$ch]
        if (-not $value) {
            $total = -1
            break
        }

        $total += $value
        if ($prevValue -gt 0 -and $value -gt $prevValue) {
            $total -= 2 * $prevValue
            $prevValue = 0
        } else {
            $prevValue = $value
        }
    }

    $total
}

if ($args.Length -lt 1) {
    Show-Usage
}

$result = RomanNumeral $args[0]
if ($result -lt 0) {
    Show-Error
}

Write-Output $result

Roman Numeral in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.