Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- /// <summary>
- /// 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.
- /// </summary>
- public class DecToBin
- {
- public static void ConvertToBinary(long input)
- {
- string output = "";
- long result = 0;
- List<string> inputBinary = new List<string>();
- while (input >= 1)
- {
- result = input % 2;
- input /= 2;
- inputBinary.Add(Convert.ToString(result));
- }
- inputBinary.Reverse();
- foreach (var num in inputBinary)
- {
- output += num;
- }
- Console.WriteLine(output);
- }
- static void Main()
- {
- long input = Int64.Parse(Console.ReadLine());
- ConvertToBinary(input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement