Advertisement
valent1n

Homework NumeralSystems - Task 01

Jan 5th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.53 KB | None | 0 0
  1. using System;
  2.  
  3. class DecimalToBinary
  4. {
  5.     // 01. Write a program to convert decimal numbers to their binary representation.
  6.  
  7.     static void Main()
  8.     {
  9.         int number = 9;
  10.  
  11.         Console.WriteLine("\n{0}d -> {1}b", number, ConvertDecToBin(number));
  12.     }
  13.  
  14.     static string ConvertDecToBin(int num)
  15.     {
  16.         string binaryCode = string.Empty;
  17.  
  18.         for (int i = num; i >= 1; i >>= 1)
  19.         {
  20.             binaryCode = (i - (i >> 1 << 1)) + binaryCode;
  21.         }
  22.  
  23.         return binaryCode;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement