Introduction
A binary calculator is a simple tool used to perform arithmetic operations (addition, subtraction, multiplication, and division) on binary numbers. Binary numbers are represented in the base-2 numeral system, consisting only of the digits 0 and 1. This binary calculator will allow users to input binary numbers and perform basic arithmetic operations using Python programming language.
Objective
The main objective of this program is to build a binary calculator that can:
- Accept binary inputs.
- Perform binary arithmetic operations (addition, subtraction, multiplication, division).
- Display the result in binary format.
Python Code
def binary_calculator(): print("Welcome to Binary Calculator!") # Accept user input for two binary numbers bin1 = input("Enter the first binary number: ") bin2 = input("Enter the second binary number: ") # Convert binary inputs to decimal integers num1 = int(bin1, 2) num2 = int(bin2, 2) # Display available operations print("\nChoose the operation you want to perform:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") # Accept user input for operation choice choice = input("\nEnter choice (1/2/3/4): ") if choice == '1': result = num1 + num2 operation = "Addition" elif choice == '2': result = num1 - num2 operation = "Subtraction" elif choice == '3': result = num1 * num2 operation = "Multiplication" elif choice == '4': if num2 == 0: print("Error! Division by zero.") return result = num1 // num2 operation = "Division" else: print("Invalid input. Please select a valid operation.") return # Convert result to binary bin_result = bin(result)[2:] # Display the result print(f"\n{operation} result:") print(f"{bin1} ({num1}) and {bin2} ({num2}) => {bin_result} ({result}) in binary and decimal respectively.") # Run the binary calculator binary_calculator()
Explanation of Program Structure
The program starts by printing a welcome message and then accepts two binary numbers from the user. These binary numbers are converted to decimal integers using Python’s int(bin_number, 2)
function.
The user is then prompted to choose an arithmetic operation (addition, subtraction, multiplication, or division). Depending on the user’s choice, the corresponding operation is performed on the decimal equivalents of the input binary numbers.
After performing the operation, the result is converted back into binary format using bin(result)[2:]
, which converts the result to binary and strips off the “0b” prefix.
Finally, the result is displayed both in binary and decimal form.
How to Run the Program
To run the binary calculator program, follow these steps:
- Ensure that Python is installed on your system.
- Open a text editor and copy the Python code provided above.
- Save the code with a
.py
extension, for example,binary_calculator.py
. - Open a terminal or command prompt window.
- Navigate to the directory where the Python file is saved.
- Run the program by typing
python binary_calculator.py
in the terminal and pressing Enter.