Python

 

 

 

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:

  1. Ensure that Python is installed on your system.
  2. Open a text editor and copy the Python code provided above.
  3. Save the code with a .py extension, for example, binary_calculator.py.
  4. Open a terminal or command prompt window.
  5. Navigate to the directory where the Python file is saved.
  6. Run the program by typing python binary_calculator.py in the terminal and pressing Enter.

 

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)