Advertisement
ViValDam

Untitled

Apr 3rd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. using System;
  2. class DecimalToBinaryNumber
  3. {
  4. /*Problem 14. Decimal to Binary Number
  5. Using loops write a program that converts an integer number to its binary representation.
  6. The input is entered as long.
  7. The output should be a variable of type string.
  8. Do not use the built-in .NET functionality.
  9. Examples:
  10. decimal binary
  11. 0 0
  12. 3 11
  13. 43691 1010101010101011
  14. 236476736 1110000110000101100101000000
  15. */
  16. static void Main()
  17. {
  18. while (true)
  19. {
  20. //IN
  21. Console.Write("Decimal : ");
  22. string str = Console.ReadLine();
  23. long numberDecimal = long.Parse(str);
  24.  
  25. //OUT
  26. Console.Write("Binary : ");
  27. if (numberDecimal == 0)
  28. {
  29. str = "0";
  30. }
  31. else
  32. {
  33. str = "";
  34. long[]rest = new long[64];
  35. int i = 0;
  36. while (numberDecimal > 0)
  37. {
  38. rest[i] = numberDecimal % 2;
  39. numberDecimal = numberDecimal / 2;
  40. i++;
  41. }
  42. i = i - 1;
  43. while (i >= 0)
  44. {
  45.  
  46. str = str + rest[i];
  47.  
  48. i--;
  49. }
  50. }
  51. Console.WriteLine(str);
  52. Console.WriteLine();
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement