Write a C# Sharp program to convert a given integer value to Roman numerals
- برمجة سي شارب
- برمجة
- 2021-05-31
- ahmadghneem
الأجوبة
using System;
using System.Text;
namespace exercises {
class Program {
static void Main(string[] args) {
int n;
n = 2365;
Console.WriteLine("Original integer value: " + n);
Console.WriteLine("Roman numerals of the said integer value:");
Console.WriteLine(int_to_Roman(n));
n = 254;
Console.WriteLine("Original integer value: " + n);
Console.WriteLine("Roman numerals of the said integer value:");
Console.WriteLine(int_to_Roman(n));
n = 45;
Console.WriteLine("Original integer value: " + n);
Console.WriteLine("Roman numerals of the said integer value:");
Console.WriteLine(int_to_Roman(n));
n = 8;
Console.WriteLine("Original integer value: " + n);
Console.WriteLine("Roman numerals of the said integer value:");
Console.WriteLine(int_to_Roman(n));
}
public static string int_to_Roman(int n)
{
string[] roman_symbol = { "MMM", "MM", "M", "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C", "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I" };
int[] int_value = { 3000, 2000, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
var roman_numerals = new StringBuilder();
var index_num = 0;
while (n != 0)
{
if (n >= int_value[index_num])
{
n -= int_value[index_num];
roman_numerals.Append(roman_symbol[index_num]);
}
else
{
index_num++;
}
}
return roman_numerals.ToString();
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال