Panglot

Decimal To Binary

Apr 8th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.54 KB | None | 0 0
  1. using System;
  2. class DecToBin
  3. {
  4.     static void Main()
  5.     {
  6.         Console.Write("Enter decimal number: ");
  7.         long n = long.Parse(Console.ReadLine());
  8.  
  9.         string result="";
  10.  
  11.  
  12.         for (; n!=0;n/=2)
  13.         {
  14.             if (n % 2 == 1)
  15.                 result += "1";
  16.             else
  17.                 result += "0";
  18.         }
  19.  
  20.         char[] forReverse = result.ToCharArray();
  21.         Array.Reverse(forReverse);
  22.         result = new string(forReverse);
  23.  
  24.  
  25.  
  26.         Console.WriteLine("Binary: "+result);
  27.  
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment