Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- //Write a program to convert from any numeral system of given
- //base s to any other numeral system of base d (2 ≤ s, d ≤ 16).
- class AnyNumeralSystem
- {
- static void Main()
- {
- Console.WriteLine("Enter number: ");
- string convert = Console.ReadLine();
- Console.WriteLine("Enter bace of numeral system: ");
- int bace = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter new bace of numeral system: ");
- int newBace = int.Parse(Console.ReadLine());
- //convert to decimal system
- int decimalNum = 0;
- for (int i = convert.Length-1, reverse = 0; i>= 0; i--, reverse++)
- {
- decimalNum += (convert[i]-'0') * (int)Math.Pow(bace, reverse);
- }
- //convert to new system
- StringBuilder builder = new StringBuilder();
- while (decimalNum > 0)
- {
- builder.Insert(0, decimalNum % newBace);
- decimalNum /= newBace;
- }
- Console.WriteLine("Converted number: \n{0}",builder.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement