ilmisha

Decimal to Binary Num

Dec 11th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. //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:
  2. //decimal binary
  3. //0 0
  4. //3 11
  5. //43691 1010101010101011
  6. //236476736 1110000110000101100101000000
  7.  
  8. using System;
  9. class DecimalToBinaryNum
  10. {
  11. static void Main()
  12. {
  13. long input = long.Parse(Console.ReadLine());
  14. string binNum = "";
  15. string[] binArray = new string[50];
  16.  
  17. for (int i = 0; input > 0; i++)
  18. {
  19. if (input == 1)
  20. {
  21. binNum = "1";
  22. }
  23. else
  24. {
  25. if (input % 2 == 0)
  26. {
  27. binNum = "0";
  28. }
  29. else
  30. {
  31. binNum = "1";
  32. }
  33. }
  34. input = input / 2;
  35. binArray[i] = binNum;
  36. }
  37.  
  38. Array.Reverse(binArray);
  39. foreach (var decDigit in binArray)
  40. {
  41. Console.Write(decDigit);
  42. }
  43. Console.WriteLine();
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment