Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _06_NToPSystem
  4. {
  5.     public class Program
  6.     {
  7.         public static void Main()
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int p = int.Parse(Console.ReadLine());
  11.  
  12.             string input;
  13.             while ((input = Console.ReadLine()) != "END")
  14.             {
  15.                 int decimalResult = 0;
  16.  
  17.                 for (int i = 0; i < input.Length; i++)
  18.                 {
  19.                     int currentNumber = (int)char.GetNumericValue(input[input.Length - i - 1]);
  20.  
  21.                     decimalResult += currentNumber * (int)Math.Pow(n, i);
  22.                 }
  23.  
  24.                 string binary = string.Empty;
  25.  
  26.                 while (decimalResult > 0)
  27.                 {
  28.                     int currentNumber = decimalResult % p;
  29.  
  30.                     if (p > 10 && currentNumber > 9)
  31.                     {
  32.                         char symbol = (char)('A' + currentNumber - 10);
  33.                         binary += symbol;
  34.                     }
  35.                     else
  36.                     {
  37.                         binary += currentNumber;
  38.                     }
  39.  
  40.                     decimalResult = decimalResult / p;
  41.                 }
  42.  
  43.                 for (int i = 0; i < binary.Length; i++)
  44.                 {
  45.                     Console.Write(binary[binary.Length - i - 1]);
  46.                 }
  47.                 Console.WriteLine();
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement