Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class DecimalToBinary
- {
- static void Main()
- {
- int decimalNumber = Input();
- BinaryRepresentation(decimalNumber);
- Main();
- }
- private static void BinaryRepresentation(int decimalNumber)
- {
- int remainder;
- string result = string.Empty;
- if (decimalNumber == 0)
- {
- result = "0000";
- }
- else
- {
- while (decimalNumber > 0)
- {
- remainder = decimalNumber % 2;
- decimalNumber /= 2;
- result = remainder.ToString() + result;
- }
- }
- Console.WriteLine("Binary representation: {0}" + Environment.NewLine, result);
- }
- private static int Input()
- {
- int decimalNumber;
- do
- {
- string message = "Please enter value between 0 and " + int.MaxValue + " !";
- Console.WriteLine(message);
- Console.Write("Enter decimal number: ");
- } while (!(int.TryParse(Console.ReadLine(), out decimalNumber) && decimalNumber >= 0 && decimalNumber <= int.MaxValue));
- return decimalNumber;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment