Advertisement
Asinka

AnyNumericSystem

Jan 17th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. class AnyNumeralSystem
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Enter base S to use:");
  10.         int baseNumeralSystemS = int.Parse(Console.ReadLine());
  11.         Console.WriteLine("\nEnter base D to convert:");
  12.         int baseNumeralSystemD = int.Parse(Console.ReadLine());
  13.         Console.WriteLine("\nEnter number:");
  14.         string number = Console.ReadLine();
  15.         int digit = 0;
  16.         int sum = 0;
  17.         int startIndex = number.Length - 1;
  18.  
  19.         for (int i = 0; i < number.Length; i++)
  20.         {
  21.             string part = number.Substring(startIndex, 1);  
  22.             digit = Convert.ToInt32(part) * (int)Math.Pow(baseNumeralSystemS, i);
  23.             sum = sum + digit;
  24.             startIndex--;
  25.         }
  26.         Console.WriteLine("\nShowing the result:");
  27.  
  28.         int count = 0;
  29.         for (int i = 31; i >= 0; i--)  
  30.         {
  31.             int exponent = (int)Math.Pow(baseNumeralSystemD, i);
  32.             digit = sum / exponent;
  33.             sum = sum % exponent;
  34.             if (digit != 0)
  35.             {
  36.                 count++;
  37.             }
  38.             if (count > 0)
  39.             {
  40.                 Console.Write(digit);
  41.             }
  42.         }
  43.         Console.WriteLine("\n");
  44.  
  45.        
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement