- namespace CSharpPractice
- {
- class Class1
- {
- public static void RomanDecimalConvertor(int num)
- {
- Dictionary<int, string> roman = new Dictionary<int, string>
- {
- {1,"I"}, {4,"IV"},
- {5,"V"}, {9,"IX"},
- {10,"X"}, {40,"XL"},
- {50,"L"}, {90,"XC"},
- {100,"C"}, {400,"CD"},
- {500,"D"}, {900,"CM"},
- {1000,"M"}
- };
- int divisor = 0, times = 0;
- while(num!=0)
- {
- foreach (int key in roman.Keys)
- {
- if (key > num)
- break;
- divisor = key;
- }
- times = num / divisor;
- for (int i = 1; i <= times; i++)
- {
- Console.Write(roman[divisor]);
- }
- num = num % divisor;
- }
- }
- }
- }
- namespace CSharpPractice
- {
- class Program
- {
- static void Main(string[] args)
- {
- Class1.RomanDecimalConvertor(1500);
- Console.Read();
- }
- }
- }
Monday, 22 September 2014
Convert Decimal To Roman using Dictionary
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment