Write a C# Sharp program to convert a given integer value to Roman numerals

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to convert a given integer value to Roman numerals.
Expected Output:
Original integer value: 2365
Roman numerals of the said integer value:
MMCCCLXV
Original integer value: 254
Roman numerals of the said integer value:
CCLIV
Original integer value: 45
Roman numerals of the said integer value:
XLV
Original integer value: 8
Roman numerals of the said integer value:
VIII

الأجوبة

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();
        }
    }
  }
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...