'C# Convert Integer into Roman Numeral and Number in Words
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.
Solution 1:[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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |