Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- using System;
- class DecimalToBinaryNum
- {
- static void Main()
- {
- long input = long.Parse(Console.ReadLine());
- string binNum = "";
- string[] binArray = new string[50];
- for (int i = 0; input > 0; i++)
- {
- if (input == 1)
- {
- binNum = "1";
- }
- else
- {
- if (input % 2 == 0)
- {
- binNum = "0";
- }
- else
- {
- binNum = "1";
- }
- }
- input = input / 2;
- binArray[i] = binNum;
- }
- Array.Reverse(binArray);
- foreach (var decDigit in binArray)
- {
- Console.Write(decDigit);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment