Advertisement
wingman007

AlgorithmsDataStructuresConvert2BinaryAngelZlatanov

Oct 10th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. // Angel Zlatanov <angel.zlatanow@gmail.com>
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. namespace ConsoleApp2
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             bool check;
  13.             int integer1=0;
  14.             do
  15.             {
  16.                 check = true;
  17.                 Console.Write("\nМоля въведете число: ");
  18.                 string string1 = Console.ReadLine();
  19.                 check = Int32.TryParse(string1, out integer1);
  20.             } while (!check);
  21.  
  22.             List<int> ostatuci = new List<int>();
  23.             int counter = 0;
  24.             do
  25.             {
  26.                 if(integer1%2!=0) ostatuci.Add(1);
  27.                 else ostatuci.Add(0);
  28.  
  29.                 integer1 = integer1 / 2;
  30.  
  31.                 counter++;
  32.             } while (integer1>=2);
  33.             ostatuci.Add(1);
  34.             counter++;
  35.  
  36.             Console.Write("Бинарно значение: ");
  37.             for(int i=counter-1; i>=0; i--) Console.Write(ostatuci[i]);
  38.             // Console.WriteLine("\nБинарно значение: " + Convert.ToString(integer1, 2));
  39.             // Console.WriteLine("\nHex: " + Convert.ToInt32(Convert.ToString(integer1, 2), 2).ToString("X"));
  40.  
  41.  
  42.  
  43.             Console.ReadKey();
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement