How to resolve the algorithm Roman numerals/Encode step by step in the Swift programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Roman numerals/Encode step by step in the Swift programming language

Table of Contents

Problem Statement

Create a function taking a positive integer as its parameter and returning a string containing the Roman numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately, starting with the left most digit and skipping any digit with a value of zero.

In Roman numerals:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Roman numerals/Encode step by step in the Swift programming language

Source code in the swift programming language

func ator(var n: Int) -> String {

    var result = ""
    
    for (value, letter) in
       [( 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")]
    {
        while n >= value {
            result += letter
            n   -= value
        }
    }
    return result
}

println(ator(1666)) // MDCLXVI

print(ator(1666)) // MDCLXVI

  

You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Ruby programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Fortran programming language
You may also check:How to resolve the algorithm Golden ratio/Convergence step by step in the ObjectIcon programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Rhovas programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the Erlang programming language