Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.60 KB | None | 0 0
  1. using System;
  2. class MainClass {
  3.   public static void Main (string[] args) {
  4.     int num = Convert.ToInt32(Console.ReadLine());
  5.     int arrayLength = (int) Math.Log(num,2) + 1; // symbols in number in binary format
  6.     int[] array = new int[arrayLength]; //array to hold number in binary format
  7.     int counter = 0;
  8.     while(num > 0)
  9.     {
  10.       array[counter++] = num % 2;
  11.       num = num / 2;
  12.     }
  13.     long binaryNum = 0; //variable to hold number in binary format
  14.     for(int i = arrayLength-1 ; i >=0; --i)
  15.       binaryNum = binaryNum*10 + array[i];
  16.      
  17.     Console.WriteLine(binaryNum);
  18.   }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement