Advertisement
MiroslavKisov

DecimalToBinaryConverterWithStack

Jan 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DecimalToBinaryConverter
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             var inputDecimal = int.Parse(Console.ReadLine());
  11.             var stackBinary = new Stack<int>();
  12.             if (inputDecimal == 0)
  13.             {
  14.                 Console.WriteLine(0);
  15.                 return;
  16.             }
  17.             while(inputDecimal != 0)
  18.             {
  19.                 var currentNumber = inputDecimal % 2;
  20.                 inputDecimal /= 2;
  21.                 stackBinary.Push(currentNumber);
  22.             }
  23.             var count = stackBinary.Count;
  24.             for (int i = 0; i < count; i++)
  25.             {
  26.                 Console.Write(stackBinary.Pop());
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement