Decimal to roman numeral converter in c++

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

// Include header file
#include <iostream>
#include <string>
#include <vector>

//  stdc++11 program for
//  Conversion from Decimal to roman number
class Perform
{
    // Display roman value of n
    public:
    static void result(int n)
    {
        switch (n){
            // Test Cases
            case 1:
                std::cout << "I";
                break;
            case 4:
                std::cout << "IV";
                break;
            case 5:
                std::cout << "V";
                break;
            case 9:
                std::cout << "IX";
                break;
            case 10:
                std::cout << "X";
                break;
            case 40:
                std::cout << "XL";
                break;
            case 50:
                std::cout << "L";
                break;
            case 90:
                std::cout << "XC";
                break;
            case 100:
                std::cout << "C";
                break;
            case 400:
                std::cout << "CD";
                break;
            case 500:
                std::cout << "D";
                break;
            case 900:
                std::cout << "DM";
                break;
            case 1000:
                std::cout << "M";
                break;
                    }
    }
    static long select(long number, 
        std::vector<int> collection, 
        int size)
    {
        int n = 1;
        int i = 0;
        for (
        i = 0; i < size; ++i)
        {
            if (number >= collection[i])
            {
                n = collection[i];
            }
            else
            {
                break;
            }
        }
        Perform::result(n);
        // Reduce the value of number
        return number - n;
    }
    static void romanNo(long number)
    {
        if (number <= 0)
        {
            // When is not a natural number
            return;
        }
        // Base case collection
      	// We used an vector you can do with an array
        std::vector<int> collection{1, 4, 5, 9, 10, 
          40, 50, 90, 100, 400, 500, 900, 1000};
        // Get the size of collection
        int size = collection.size();
        std::cout << " " + std::to_string(number) + " : ";
        while (number > 0)
        {
            number = Perform::select(number, collection, size);
        }
        // Add new line
        std::cout << std::endl;
    }

};
int main(int argc, char **argv){
    // Test Case
    Perform::romanNo(10);
    Perform::romanNo(18);
    Perform::romanNo(189);
    Perform::romanNo(604);
    Perform::romanNo(982);
    Perform::romanNo(3000);
    return 0;
};

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







© 2022, kalkicode.com, All rights reserved