Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class DecimalToBinaryNumber
- {
- /*Problem 14. Decimal to Binary Number
- Using loops write a program that converts an integer number to its binary representation.
- The input is entered as long.
- The output should be a variable of type string.
- Do not use the built-in .NET functionality.
- Examples:
- decimal binary
- 0 0
- 3 11
- 43691 1010101010101011
- 236476736 1110000110000101100101000000
- */
- static void Main()
- {
- while (true)
- {
- //IN
- Console.Write("Decimal : ");
- string str = Console.ReadLine();
- long numberDecimal = long.Parse(str);
- //OUT
- Console.Write("Binary : ");
- if (numberDecimal == 0)
- {
- str = "0";
- }
- else
- {
- str = "";
- long[]rest = new long[64];
- int i = 0;
- while (numberDecimal > 0)
- {
- rest[i] = numberDecimal % 2;
- numberDecimal = numberDecimal / 2;
- i++;
- }
- i = i - 1;
- while (i >= 0)
- {
- str = str + rest[i];
- i--;
- }
- }
- Console.WriteLine(str);
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement