Advertisement
kuruku

DecimalToBinaryNumber

May 3rd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System;
  2.  
  3. //Using loops write a program that converts an integer number to its binary representation. The input is entered as long.
  4. //The output should be a variable of type string. Do not use the built-in .NET functionality.
  5.  
  6. //  decimal             binary
  7. //  0                       0
  8. //  3                       11
  9. //  43691               1010101010101011
  10. //  236476736               1110000110000101100101000000
  11.  
  12.     class DecimalToBinaryNumber
  13.     {
  14.         static void Main()
  15.         {
  16.             int n = int.Parse(Console.ReadLine());
  17.             string nbinary = null;
  18.             int r = 0;
  19.             while (n > 0)
  20.             {
  21.                 r = n % 2;
  22.                 n /= 2;
  23.                 nbinary += r;
  24.             }
  25.             Console.WriteLine(nbinary);
  26.             string nBinary2 = null;
  27.             for (int i = nbinary.Length - 1; i >= 0; i--)
  28.             {
  29.                 nBinary2 += nbinary[i];
  30.  
  31.             }
  32.             Console.WriteLine(nBinary2);
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement