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:
- Take the values of two numbers from the user using the int(input()) function and store it in two variable say num1 and num2
- Call function add and pass the values of num1 and num2 as an argument to the add function.
- Call function sub and pass the values of num1 and num2 as an argument to the sub function.
- Call function mul and pass the values of num1 and num2 as an argument to the mul function.
- Call function div and pass the values of num1 and num2 as an argument to the div function.
- Copy the values of num1 and num2 at variable n1,n2.
- 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.
- 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.
- 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.
- 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.
- 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
- Python Program to read text file and find out longest word from file
- Python Program to count number of words in a file
- Python program to write those lines which have the character ‘P’ from one text file to another text file
- Python program to get file size in bytes
- Python program to read file line by line and store all the contents of file into array