How to make a calculator using python ?

 It's a good idea to make a calculator using python. It is very easy and useful while in the online class or online math exam (joking). Before That let us know a little about all IDE(Integrated development environment). Don't get confused here by the name. It is simply a software application that provides complete facilities to computer programmers for software development or program development. 



So we are here trying to make a program that will give us a sum of 2 numbers, Subtract between 2 numbers, Multiplying 2 numbers, and Divide 2 numbers. 


Now let's start programming…..

  • Using functions add(), subtract(), multiply() and divide() evaluate respective operations.



    Code:  

    #Function to Add


    def add(num1, num2):
        return num1 + num2

    #Function to subtract


    def subtract(num1, num2):
        return num1 - num2
      

    #Function to multiply

    def multiply(num1, num2):

        return num1 * num2

     #Function to Divide
    def divide(num1, num2):
        return num1 / num2
      
    print("Please select operation -\n" \
            "1. Add\n" \
            "2. Subtract\n" \
            "3. Multiply\n" \
            "4. Divide\n")
      
      

    select = int(input("Select operations form 1, 2, 3, 4 :"))
      
    number_1 = int(input("Enter first number: "))
    number_2 = int(input("Enter second number: "))
      
    if select == 1:
        print(number_1, "+", number_2, "=",
                        add(number_1, number_2))
      
    elif select == 2:
        print(number_1, "-", number_2, "=",
                        subtract(number_1, number_2))
      
    elif select == 3:
        print(number_1, "*", number_2, "=",
                        multiply(number_1, number_2))
      
    elif select == 4:
        print(number_1, "/", number_2, "=",
                        divide(number_1, number_2))
    else:
        print("Invalid input")


    Output :

    Please select operation -
        1. Add
        2. Subtract
        3. Multiply
        4. Divide
        Select operations form 1, 2, 3, 4 : 1
        Enter first number : 15
        Enter second number : 14
        15 + 14 = 29
        
    I hope all those who were reading have understood this concept very well.....
  • Thank You For Visiting......

  • Post a Comment

    0 Comments