Advertisement
Sekklow

Loops-DecimalToBinary

Aug 17th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. /// <summary>
  5. /// Using loops write a program that converts an integer number to its
  6. /// binary representation. The input is entered as long. The output
  7. /// should be a variable of type string.
  8. /// </summary>
  9. public class DecToBin
  10. {
  11.     public static void ConvertToBinary(long input)
  12.     {
  13.         string output = "";
  14.         long result = 0;
  15.         List<string> inputBinary = new List<string>();
  16.         while (input >= 1)
  17.         {
  18.             result = input % 2;
  19.             input /= 2;
  20.             inputBinary.Add(Convert.ToString(result));
  21.         }
  22.         inputBinary.Reverse();
  23.         foreach (var num in inputBinary)
  24.         {
  25.             output += num;
  26.         }
  27.         Console.WriteLine(output);
  28.     }
  29.  
  30.     static void Main()
  31.     {
  32.         long input = Int64.Parse(Console.ReadLine());
  33.  
  34.         ConvertToBinary(input);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement