Example HTML page

A Python program to perform arithmetic operations using functions.

In this post i am providing the example programs of different arithmetic operations such as Addition, Subtraction, Multiplication, Division and Modulo using functions.

The following program performs addition of two numbers by using functions. It is also being considered as an example program of user-defined functions.

def add_numbers(a,b):
   sum = a + b
   return sum
num1 = 5
num2 = 6
print("5"+"+"+"6"+"=", add_numbers(num1, num2))

Output

5+6= 11

Explanation

Initially we are defining the function with name add_numbers. Here we are passing two parameters inside the function are a and b. Next we are creating a variable ‘sum’ to store the result of a+b.  The return statement returns the ‘sum’ result. We are defining two variables num1 and num2  and assign some value to them.  Finally we can print the result by passing the two parameters num1 and num 2 to the function definition.  After passing the values of num 1 and num 2 are allocated to a and b respectively and then the addition operation is performed and finally prints the result.

Similarly, we can code for subtraction,multiplication,division and modulo.

The following program performs subtraction of two numbers by using functions.

def sub_numbers(a,b):
   sub = a - b
   return sub
num1 = 20
num2 = 10
print("20"+"-"+"10"+"=", sub_numbers(num1, num2))

Output

20-10= 10

The following program performs multiplication of two numbers by using functions.

def mul_numbers(a,b):
   mul = a * b
   return mul
num1 = 7
num2 = 6
print("7"+"*"+"6"+"=", mul_numbers(num1, num2))

Output

7*6= 42

The following program performs division of two numbers by using functions.

def div_numbers(a,b):
   div = a / b
   return div
num1 = 15
num2 = 3
print("15"+"/"+"3"+"=", div_numbers(num1, num2))

Output

15/3= 5.0

The following program performs modulo of two numbers by using functions.

def mod_numbers(a,b):
   mod = a % b
   return mod
num1 = 10
num2 = 4
print("10"+"%"+"4"+"=", mod_numbers(num1, num2))

Output

10%4= 2
Example HTML page

Leave a Reply

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

Pin It on Pinterest