Sum of Two Numbers in Python utilizing Perform


report this advert

We are going to develop a program to find the sum of two numbers in python utilizing perform. We are going to give two numbers num1 and num2. Python packages will add these numbers utilizing the arithmetic operator (+). We will even develop a python program so as to add two numbers with out utilizing + operator.

Add Two Numbers in Python utilizing Perform

We are able to take the assistance of a perform so as to add two numbers in python. A perform is a block of code that performs a selected process. We are going to take two numbers whereas declaring the variables. Then, name the perform and the sum of numbers can be displayed on the display screen.

Program description:- write a program so as to add two numbers in python

# Python program so as to add two numbers utilizing perform

def add_num(a,b):   #user-defined perform
   sum = a + b   #including numbers
   return sum   #return worth

# take inputs
num1 = 3
num2 = 7

# calling perform
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 seven is 10

Within the earlier program, inputs are hardcoded in this system however on this program, inputs can be supplied by the person.

# Python program so as to add two numbers utilizing perform

def add_num(a,b):   #user-defined perform
   sum = a + b   #including numbers
   return sum   #return worth

# take inputs
num1 = float(enter('Enter first quantity: '))
num2 = float(enter('Enter second quantity: '))

# calling perform
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 enter values test-case-1:-

Enter first quantity: 6
Enter second quantity: 2
The sum of numbers 6.0 and a couple of.0 is 8.0

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

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

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

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

Sum of Two Numbers in Python utilizing Perform

This python program additionally performs the identical process however in numerous methods. On this program, we add two numbers with out utilizing (+) operator.

# Python program so as to add two numbers utilizing perform

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

# take inputs
num1 = float(enter('Enter first quantity: '))
num2 = float(enter('Enter second quantity: '))

# calling perform
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 enter values test-case-1:-

Enter first quantity: 5
Enter second quantity: 3
The sum of numbers 5.0 and three.0 is 8.0

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

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

# Python program so as to add two numbers utilizing perform

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

# take inputs
num1 = int(enter('Enter first quantity: '))
num2 = int(enter('Enter second quantity: '))

# calling perform
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 enter values test-case-1:-

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

Leave a Reply

Your email address will not be published.