Arithmetic Operations using function in python

Write a Python Program to Perform Arithmetic Operations on numeric values using function with a practical example.

Program Description:

The program takes two numbers  from user and  and  perform the Arithmetic Operations such as Addition, Subtraction, Multiplication,Modulus, and Division using user defined function

Program Logic:

  1. Take the values of two numbers from the user using the int(input()) function and store it in two variable say num1 and num2
  2. Call function add and pass the values of num1 and num2 as an argument to the add function.
  3. Call function sub and pass the values of num1 and num2 as an argument to the sub function.
  4. Call function mul and pass the values of num1 and num2 as an argument to the mul function.
  5. Call function div and pass the values of num1 and num2 as an argument to the div function.
  6. Copy the values of num1 and num2 at variable n1,n2.
  7. Create a user defined functions to say add, which takes the values of n1 and n2 as an argument using def keyword and return calculated result.
  8. Create a user defined functions to say sub, which takes values of n1 and n2 as an argument using def keyword and return calculated result.
  9. Create a user defined functions to say mul, which takes values of n1 and n2  as an argument using def keyword and return calculated result.
  10. Create a  user defined functions to say div,  which takes values the of n1 and n2 as an argument using def keyword and return calculated result.
  11. Print the result as output and exit.

Below is Implementation code/Source Code :

Here is source code of the Python Program to Perform Arithmetic Operations on numeric values using function with a practical example.

# program to perform arithmetic operation on numeric values using function
#Addition operation
def add(n1, n2):
    return n1+n2


#Substraction operation
def minus(n1, n2): 
    return n1 - n2

#Multiplication operation
def multiply(n1, n2):  
    return n1 * n2

#Division operation
def divide(n1, n2):
    return n1 / n2
num1= int(input("Enter First number :"))
num2 = int(input("Enter Second number :"))
print("+++++++++Addition +++++++++++++")
print(num1,"+" ,num2,"=", add(num1,num2))
print("--------------Substraction-----------")
print(num1,"-" ,num2,"=", minus(num1, num2))
print("***************Multiplication************")
print(num1,"*" ,num2,"=",multiply(num1, num2))
print("///////////////Division//////////////////")
print(num1,"/", num2,"=",divide(num1, num2))


Output:

Enter First number :10
Enter Second number :5
+++++++++Addition +++++++++++++
10 + 5 = 15
————–Substraction———–
10 – 5 = 5
***************Multiplication************
10 * 5 = 50
///////////////Division//////////////////
10 / 5 = 2.0

You can also check this too

  1. Python Program to read text file and find out longest word from file
  2. Python Program to count number of words in a file
  3. Python program to write those lines which have the character ‘P’ from one text file to another text file
  4. Python program to get file size in bytes
  5. Python program to read file line by line and store all the contents of file into array


<