Skip to main content

Decimal to roman numeral converter in swift

Swift program for Decimal to roman numeral converter. Here mentioned other language solution.

import Foundation
//  Swift program for
//  Conversion from Decimal to roman number
class Perform
{
    // Display roman value of n
    static func result(_ n : Int)
    {
        switch(n) {
            // Test Cases
            case 1:
                print("I",terminator: "");
            case 4:
                print("IV",terminator: "");
            case 5:
                print("V",terminator: "");
            case 9:
                print("IX",terminator: "");
            case 10:
                print("X",terminator: "");
            case 40:
                print("XL",terminator: "");
            case 50:
                print("L",terminator: "");
            case 90:
                print("XC",terminator: "");
            case 100:
                print("C",terminator: "");
            case 400:
                print("CD",terminator: "");
            case 500:
                print("D",terminator: "");
            case 900:
                print("DM",terminator: "");
            case 1000:
                print("M",terminator: "");
            default:
                return;
        }
    }
    static func select(_ number : Int, _ collection : [Int], _ size : Int) -> Int
    {
        var n : Int = 1;
        var i : Int = 0;
        do
        {
            i = 0;
            while (i < size)
            {
                if (number >= collection[i])
                {
                    n = collection[i];
                }
                 else
                {
                    break;
                }
                i += 1;
            }
        }
        Perform.result(n);
        // Reduce the value of number
        return number - n;
    }
    static func romanNo(_ num : Int)
    {
      	var number = num;
        if (number <= 0)
        {
            // When is not a natural number
            return;
        }
        // Base case collection
        let collection : [Int] =
        [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
        // Get the size of collection
        let size : Int = collection.count;
        print(" \(number) ",terminator: ": ");
        while (number > 0)
        {
            number = Perform.select(number, collection,size);
        }
        // Add new line
        print();
    }
    static func main(_ args : inout [String])
    {
        // Test Case
        Perform.romanNo(10);
        Perform.romanNo(18);
        Perform.romanNo(189);
        Perform.romanNo(604);
        Perform.romanNo(982);
        Perform.romanNo(3000);
    }
}

var record : [String] = CommandLine.arguments;
Perform.main(&record);

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment