0

I need a help because I've created a program wherein if the user inputs enter an integer number then it will convert the number into Roman Numerals and in Words. But here in my codes, it's only converting it into roman numerals. How can I include literal words of the user input integer into words below the roman numeral. For example: Enter a number: 1 Roman Numeral: I Number in Words: One

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_Activity_2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
            string[] decs = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
            string[] cents = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
            string[] mills = { "", "M", "MM", "MMM" };

            Console.WriteLine("Enter a number: ");
            int n = int.Parse(Console.ReadLine());

            if (!(n >= 1 && n <= 3999))
            {
                Console.WriteLine("Number not valid!");
                return;
            }

            int m = n / 1000;
            int mig = n % 1000;
            int c = mig / 100;
            mig = mig % 100;
            int d = mig / 10;
            mig = mig % 10;
            int u = mig;

            Console.WriteLine(mills.ElementAtOrDefault(m) + cents.ElementAtOrDefault(c) + decs.ElementAtOrDefault(d) + units.ElementAtOrDefault(u));

        }
    }
}

I tried to convert the numbers into roman numerals but I don't know how to code the literal words of an integer and where to put it.

  • 1
    If user enters 3115, what should "Number In Words" output? – Xiang Wei Huang Mar 23 '22 at 07:54
  • 1
    Does this answer your question? [How can I convert an integer into its verbal representation?](https://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation) – Martheen Mar 23 '22 at 07:57
  • That heavily depends on the language you are targeting. For instance french, Italian and German work totally different in certain ranges – derpirscher Mar 23 '22 at 07:58
  • @XiangWeiHuang "Three thousand one hundred fifteen" ? – Josip Juros Mar 23 '22 at 07:58
  • @XiangWeiHuang The output would be like this: Enter a number: 3115 Roman Numeral: MMMCXV Number in Words: Three Thousand One Hundred Fifteen – Miguel Arcangel Castillo Mar 23 '22 at 08:06
  • 2
    Then the link @Martheen posted already answered your question. But the questions are actually similar logics, if you can translate numbers into Roman numeral, then you already can translate it into English words too. If you can, try do it with your ideas first, then check others' answers so you can learn. Good luck. – Xiang Wei Huang Mar 23 '22 at 08:37

1 Answers1

-1

Try following IntToRoman() and RomanToInt() APIs:

You can also find other solution via leetcode discussion tab :

https://leetcode.com/problems/integer-to-roman/

https://leetcode.com/problems/roman-to-integer/

    /// 12. Integer to Roman
    /// Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
    /// I             1
    /// V             5
    /// X             10
    /// L             50
    /// C             100
    /// D             500
    /// M             1000
    /// CM            900
    /// CD            400
    /// XC            90
    /// XL            40
    /// IX            9
    /// IV            4
    public string IntToRoman(int num)
    {
        if (num >= 1000) return "M" + IntToRoman(num - 1000);
        if (num >= 900) return "CM" + IntToRoman(num - 900);
        if (num >= 500) return "D" + IntToRoman(num - 500);
        if (num >= 400) return "CD" + IntToRoman(num - 400);
        if (num >= 100) return "C" + IntToRoman(num - 100);
        if (num >= 90) return "XC" + IntToRoman(num - 90);
        if (num >= 50) return "L" + IntToRoman(num - 50);
        if (num >= 40) return "XL" + IntToRoman(num - 40);
        if (num >= 10) return "X" + IntToRoman(num - 10);
        if (num >= 9) return "IX" + IntToRoman(num - 9);
        if (num >= 5) return "V" + IntToRoman(num - 5);
        if (num >= 4) return "IV" + IntToRoman(num - 4);
        if (num > 1) return "I" + IntToRoman(num - 1);
        if (num == 1) return "I";
        return string.Empty;
    }

    ///13. Roman to Integer
    ///Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
    ///It is guaranteed that s is a valid roman numeral in the range [1, 3999]
    public int RomanToInt(string s)
    {
        Dictionary<char, int> dictionary = new Dictionary<char, int>()
        {
            { 'I', 1},
            { 'V', 5},
            { 'X', 10},
            { 'L', 50},
            { 'C', 100},
            { 'D', 500},
            { 'M', 1000}
        };

        int number = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (i + 1 < s.Length && dictionary[s[i]] < dictionary[s[i + 1]])
            {
                number -= dictionary[s[i]];
            }
            else
            {
                number += dictionary[s[i]];
            }
        }
        return number;
    }
Bob
  • 107
  • 6