Sum of Two Numbers in Python using Function

We will develop a program to find the sum of two numbers in python using function. We will give two numbers num1 and num2. Python programs will add these numbers using the arithmetic operator (+). We will also develop a python program to add two numbers without using + operator.

Add Two Numbers in Python using Function

We can take the help of a function to add two numbers in python. A function is a block of code that performs a specific task. We will take two numbers while declaring the variables. Then, call the function and the sum of numbers will be displayed on the screen.

Program description:- write a program to add two numbers using function in python

# Python program to add two numbers using function

def add_num(a,b):   #user-defined function
   sum = a + b   #adding numbers
   return sum   #return value

# take inputs
num1 = 3
num2 = 7

# calling function
sum = add_num(num1, num2)

# print sum of numbers
print('The sum of numbers {0} and {1} is {2}'
                      .format(num1, num2, sum))

Output:-

The sum of numbers 3 and 7 is 10

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to add two numbers using function

def add_num(a,b):   #user-defined function
   sum = a + b   #adding numbers
   return sum   #return value

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# calling function
sum = add_num(num1, num2)

# print sum of numbers
print('The sum of numbers {0} and {1} is {2}'
                      .format(num1, num2, sum))

Output for the input values test-case-1:-

Enter first number: 6
Enter second number: 2
The sum of numbers 6.0 and 2.0 is 8.0

Output for the input values test-case-2:-

Enter first number: 25
Enter second number: 56
The sum of numbers 25.0 and 56.0 is 81.0

Output for the input values test-case-3:-

Enter first number: 8.3
Enter second number: 31
The sum of numbers 8.3 and 31.0 is 39.3

Sum of Two Numbers in Python using Function

This python program also performs the same task but in different ways. In this program, we add two numbers without using (+) operator.

# Python program to add two numbers using function

def add_num(a,b):   #user-defined function
   if a!=b:
      return (a*a-b*b)/(a-b)
   else:
      return 2*a

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# calling function
sum = add_num(num1, num2)

# print sum of numbers
print('The sum of numbers {0} and {1} is {2}'
                      .format(num1, num2, sum))

Output for the input values test-case-1:-

Enter first number: 5
Enter second number: 3
The sum of numbers 5.0 and 3.0 is 8.0

Output for the input values test-case-2:-

Enter first number: 25.82
Enter second number: 63.04
The sum of numbers 25.82 and 63.04 is 88.86

# Python program to add two numbers using function

def add_num(a,b):   #user-defined function
   while b != 0:
      c = a & b   #using and operator
      a = a ^ b   #using XOR operator
      b = c << 1
   return a

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function
sum = add_num(num1, num2)

# print sum of numbers
print('The sum of numbers {0} and {1} is {2}'
                      .format(num1, num2, sum))

Output for the input values test-case-1:-

Enter first number: 25
Enter second number: 12
The sum of numbers 25 and 12 is 37

Leave a Comment

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