Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class DecimalToBinary
- {
- // 01. Write a program to convert decimal numbers to their binary representation.
- static void Main()
- {
- int number = 9;
- Console.WriteLine("\n{0}d -> {1}b", number, ConvertDecToBin(number));
- }
- static string ConvertDecToBin(int num)
- {
- string binaryCode = string.Empty;
- for (int i = num; i >= 1; i >>= 1)
- {
- binaryCode = (i - (i >> 1 << 1)) + binaryCode;
- }
- return binaryCode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement