Xceedas

Xceedas
xceedas

Monday, 22 September 2014

Convert Decimal To Roman using Dictionary

  1. namespace CSharpPractice  
  2. {  
  3.     class Class1  
  4.     {  
  5.         public static void RomanDecimalConvertor(int num)  
  6.         {  
  7.             Dictionary<intstring> roman = new Dictionary<intstring>  
  8.                                                             {  
  9.                                                              {1,"I"}, {4,"IV"},  
  10.                                                              {5,"V"}, {9,"IX"},  
  11.                                                              {10,"X"}, {40,"XL"},  
  12.                                                              {50,"L"}, {90,"XC"},  
  13.                                                              {100,"C"}, {400,"CD"},  
  14.                                                              {500,"D"}, {900,"CM"},  
  15.                                                              {1000,"M"}  
  16.                                                             };  
  17.             int divisor = 0, times = 0;  
  18.             while(num!=0)  
  19.             {  
  20.                   
  21.                 foreach (int key in roman.Keys)  
  22.                 {  
  23.                     if (key > num)  
  24.                         break;  
  25.                     divisor = key;  
  26.                 }  
  27.                 times = num / divisor;  
  28.                 for (int i = 1; i <= times; i++)  
  29.                 {  
  30.                     Console.Write(roman[divisor]);  
  31.                 }  
  32.                 num = num % divisor;  
  33.                  
  34.             }  
  35.         }  
  36.     }  
  37. }  
  38.   
  39.   
  40.   
  41. namespace CSharpPractice  
  42. {  
  43.     class Program  
  44.     {  
  45.         static void Main(string[] args)  
  46.         {  
  47.             Class1.RomanDecimalConvertor(1500);  
  48.             Console.Read();  
  49.         }  
  50.     }  
  51. }  

No comments :