Advertisement
a1m

dec to bin modified

a1m
Mar 3rd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.         string bin = "";
  10.         string tempstring = "";
  11.         int counter = 0;
  12.  
  13.         while (n > 0)
  14.         {
  15.             tempstring += n % 2 + "";
  16.             n /= 2;
  17.             if (n == 1)
  18.             {
  19.                 tempstring += '1';
  20.                 break;
  21.             }
  22.  
  23.         }
  24.         //reversing the string
  25.         for (int i = tempstring.Length - 1; i >= 0; i--)
  26.         {
  27.             if (counter % 4 ==0 && counter!=0)
  28.             {
  29.                 bin += " ";
  30.             }
  31.             bin += tempstring[i];
  32.             counter++;            
  33.  
  34.         }
  35.         Console.WriteLine(bin);
  36.  
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement