Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- /*
- * Write a program to convert decimal numbers to their binary representation.
- */
- namespace ConvertDecimalToBinary
- {
- class ConvertDecimalToBinary
- {
- static void Main()
- {
- int decNumber = InputDecimalNumber();
- //Заб: Името е малко по-дълго за да не именувам метода като класа:
- //ако беше ConvertDecimalToBinary това щеше да е лошо именуваме защото
- // съвпада с името на конструктора на класа.
- List<int> bits = ConvertDecimalNumberToBinary(ref decNumber);
- //празен ред за да знаем, че двата реда са различни части от програмата.
- PrintBinaryNumber(bits);
- }
- private static void PrintBinaryNumber(List<int> bits)
- {
- Console.Write("The binary representation of the number is: ");
- for (int i = bits.Count - 1; i >= 0; i--)
- {
- Console.Write(bits[i]);
- }
- Console.WriteLine();
- }
- private static List<int> ConvertDecimalNumberToBinary(ref int decNumber)
- {
- List<int> bits = new List<int>();
- while (decNumber > 0)
- {
- bits.Add(decNumber % 2);
- decNumber /= 2;
- }
- return bits;
- }
- private static int InputDecimalNumber()
- {
- Console.Write("Please enter a number between 1 and {0}: ", int.MaxValue);
- int decNumber = int.Parse(Console.ReadLine());
- return decNumber;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement