Write a program for addition, subtraction, multiplication and division of two numbers in python

#include <iostream>

using namespace std;

/* This function calculating additon of two numbers. */

int AdditionOfTwoNumbers(int num1int num2)

{

    /* Calculating the sum of two numbers

    and store into sum variables */

    int sum = num1 + num2;

    return sum;

}

/* This function calculating subtraction of two numbers. */

int SubtractionOfTwoNumbers(int num1int num2)

{

    /*Calculating the subtraction of two numbers

    and store into minus variables */

    int minus = num1-num2;

    return minus;

}

/* This function calculating multiplication of two numbers. */

int MultiplicationOfTwoNumbers(int num1int num2)

{

    /* Calculating the multiplication of two numbers

    and store into mul variables */

    int mul = num1 * num2;

    return mul;

}

/* This function calculating division of two numbers. */

double DivisionOfTwoNumbers(int num1int num2)

{

    /* Calculating the division of two numbers

    and store into div variables */

    double div = (double)num1/(double)num2;

    return div;

}

int main()

{

    int num1num2;

    /* Get the input of two variables */

    cout<<"Enter the Two Numbers:";

    cin>>num1>>num2;


    /* Call Function addition_two_numbers, subtraction_two_numbers,  

     multiplication_two_numbers, and division_two_numbers with two Parameters*/

    int add = AdditionOfTwoNumbers(num1num2);

    int sub = SubtractionOfTwoNumbers(num1num2);

    int multi = MultiplicationOfTwoNumbers(num1num2);

    double division = DivisionOfTwoNumbers(num1num2);


     /* Display sum, Subtraction, Multiplication, and Division of two numbers*/

    cout<<"Addition of two Numbers is: "<<add<<endl;

    cout<<"Subtraction of two Numbers is: "<<sub<<endl;

    cout<<"Multiplication of two Numbers is: "<<multi<<endl;

    cout<<"Division of two Numbers is: "<<division<<endl;

}

Write a program for addition, subtraction, multiplication and division of two numbers in python

Python program for addition, subtraction, multiplication and division has been shown here. These are the basic arithmetic operations.

#****************************************** # alphabetacoder.com # Python program for addition, subtraction, # multiplication and division #****************************************** # take input a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) # compute operations w = a + b x = a - b y = a * b z = a / b # display result print("Addition:", w) print("Subtraction:", x) print("Multiplication:", y) print("Division:", z)

Output


Enter first number: 15.6

Enter second number: 7.5

Addition: 23.1

Subtraction: 8.1

Multiplication: 117.0

Division: 2.08


Last update on May 28 2022 13:45:51 (UTC/GMT +8 hours)

Write a Python program to add, subtract, multiply and division of two complex numbers.

Sample Solution:-

Python Code:

print("Addition of two complex numbers : ",(4+3j)+(3-7j)) print("Subtraction of two complex numbers : ",(4+3j)-(3-7j)) print("Multiplication of two complex numbers : ",(4+3j)*(3-7j)) print("Division of two complex numbers : ",(4+3j)/(3-7j))

Sample Output:

Addition of two complex numbers : (7-4j) Subtraction of two complex numbers : (1+10j) Multiplication of two complex numbers : (33-19j) Division of two complex numbers : (-0.15517241379310348+0.6379310344827587j)

Pictorial Presentation:

Write a program for addition, subtraction, multiplication and division of two numbers in python

Flowchart:

Write a program for addition, subtraction, multiplication and division of two numbers in python

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print a complex number and its real and imaginary parts.
Next: Write a Python program to get the length and the angle of a complex number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



What is the difference between Python's list methods append and extend?

append: Appends object at the end.

x = [1, 2, 3] x.append([4, 5]) print (x)

Output:

[1, 2, 3, [4, 5]]

extend: Extends list by appending elements from the iterable.

x = [1, 2, 3] x.extend([4, 5]) print (x)

Output:

[1, 2, 3, 4, 5]

Ref: https://bit.ly/2AZ6ZFq

In this article, we've created some programs in Python, that performs addition, subtraction, multiplication and division of any two numbers entered by user at run-time. Here are the list of programs:

  • Simple Add, Subtract, Multiply, and Divide Program
  • Add, Subtract, Multiply, and Divide based on User's Choice
  • Add, Subtract, Multiply, and Divide using user-defined Function
  • Using Class

Add, Subtract, Multiply, and Divide

To perform addition, subtraction, multiplication and division in Python, you have to ask from user to enter any two numbers. Based on two numbers input, the program find and prints the result for all four operations.

print("Enter First Number: ") numOne = int(input()) print("Enter Second Number: ") numTwo = int(input()) res = numOne+numTwo print("\nAddition Result = ", res) res = numOne-numTwo print("Subtraction Result = ", res) res = numOne*numTwo print("Multiplication Result = ", res) res = numOne/numTwo print("Division Result = ", res)

Here is the initial output produced by this Python program:

python add subtract multiply divide

Now let's type any two numbers say 6 as first and 3 as second number to perform addition, subtraction, multiplication, and division as shown in the snapshot given below:

addition subtractoin division multiplication python

Note - You may also like Calculator Program in Python to perform same thing in perfect way.

Modified Version of Previous Program

This is the modified version of previous program. The end is used to skip automatic printing of newline through print(). The str() method is used to convert from any type to a string type.

print("Enter Two Numbers: ", end="") nOne = int(input()) nTwo = int(input()) print("\n" +str(nOne)+ " + " +str(nTwo)+ " = " +str(nOne+nTwo)) print(str(nOne)+ " - " +str(nTwo)+ " = " +str(nOne-nTwo)) print(str(nOne)+ " * " +str(nTwo)+ " = " +str(nOne*nTwo)) print(str(nOne)+ " / " +str(nTwo)+ " = " +str(nOne/nTwo))

Here is its sample run with user input, 10 as first and 2 as second number:

subtraction python

Add, Subtract, Multiply, Divide based on User's Choice

This program performs same operations as done in previous program, but based on user's choice. That is, this program doesn't automatically prints all the four operation's result. Rather it asks from user, and will do the task based on input.

print("Enter Two Numbers: ", end="") nOne = int(input()) nTwo = int(input()) print("Enter the Operator (+,-,*,/): ", end="") ch = input() if ch=='+': print("\n" +str(nOne)+ " + " +str(nTwo)+ " = " +str(nOne+nTwo)) elif ch=='-': print("\n" +str(nOne)+ " - " +str(nTwo)+ " = " +str(nOne-nTwo)) elif ch=='*': print("\n" +str(nOne)+ " * " +str(nTwo)+ " = " +str(nOne*nTwo)) elif ch=='/': print("\n" +str(nOne)+ " / " +str(nTwo)+ " = " +str(nOne/nTwo)) else: print("\nInvalid Operator!")

Here is its sample run with user input, 10 as first and 20 as second number, / (divide) as operator:

division python

Here is another sample run with user input, 5 as first and 0 as second number, then * (multiply) as operator:

multiplication python

Add, Subtract, Multiply, Divide using Function

This program uses four user-defined functions to perform the thing. That is, this program has four functions namely add(), sub(), mul(), and div(). All functions recevies two argument and returns the corresponding result.

def add(a, b): return a+b def sub(a, b): return a-b def mul(a, b): return a-b def div(a, b): return a/b print("Enter Two Numbers: ", end="") nOne = int(input()) nTwo = int(input()) print("Enter the Operator (+,-,*,/): ", end="") ch = input() if ch=='+': print("\n" +str(nOne)+ " + " +str(nTwo)+ " = " +str(add(nOne, nTwo))) elif ch=='-': print("\n" +str(nOne)+ " - " +str(nTwo)+ " = " +str(sub(nOne, nTwo))) elif ch=='*': print("\n" +str(nOne)+ " * " +str(nTwo)+ " = " +str(mul(nOne, nTwo))) elif ch=='/': print("\n" +str(nOne)+ " / " +str(nTwo)+ " = " +str(div(nOne, nTwo))) else: print("\nInvalid Operator!")

This program produces the same output as of previous program. Here is its sample run with user input, 5 and 10 as two numbers and - as operator to perform:

python addition subtraction multiplication division

Add, Subtract, Multiply, Divide using Class

This is the last program of this article. It is created using class, an object-oriented feature of Python.

class CodesCracker: def add(self, a, b): return a+b def sub(self, a, b): return a-b def mul(self, a, b): return a-b def div(self, a, b): return a/b print("Enter Two Numbers: ", end="") nOne = int(input()) nTwo = int(input()) print("Enter the Operator (+,-,*,/): ", end="") ch = input() cobj = CodesCracker() if ch=='+': print("\n" +str(nOne)+ " + " +str(nTwo)+ " = " +str(cobj.add(nOne, nTwo))) elif ch=='-': print("\n" +str(nOne)+ " - " +str(nTwo)+ " = " +str(cobj.sub(nOne, nTwo))) elif ch=='*': print("\n" +str(nOne)+ " * " +str(nTwo)+ " = " +str(cobj.mul(nOne, nTwo))) elif ch=='/': print("\n" +str(nOne)+ " / " +str(nTwo)+ " = " +str(cobj.div(nOne, nTwo))) else: print("\nInvalid Operator!")

This program also produces similar output as of previous program.

The class CodesCracker is already defined. So using the following statement:

All the properties of CodesCracker gets assigned to cobj object. Now we can access member functions of the class CodesCracker using this object through dot (.) operator.

Short Version of Previous Program

This is the short version of previous program. In this program, we've applied the condition checking part inside the member function of the class.

class CodesCracker: def aOperation(self, a, b, c): if c=='+': return a+b elif c=='-': return a-b elif c=='*': return a*b elif c=='/': return a/b else: return 'i' print("Enter Two Numbers: ", end="") nOne = int(input()) nTwo = int(input()) print("Enter the Operator (+,-,*,/): ", end="") ch = input() cobj = CodesCracker() res = cobj.aOperation(nOne, nTwo, ch) if res=='i': print("\nInvalid Operator!") else: print("\nResult =", res)

Here is a sample run with user input 5 as first and -6 as second number, then + as operator to perform:

addition subtraction multiplication and division

Same Program in Other Languages

Python Online Test

« Previous Program Next Program »