Python Program to Add Two Numbers with User Input

We will develop a Python program to add two numbers with user input. 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 with User Input

We will take two numbers while declaring the variables and the sum of numbers will be stored to sum variable, and finally, it will be displayed on the screen.

Program description:- write a python program to accept two number from user, add the numbers and display it?

# Python program to add two numbers with user input

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

# adding numbers
sum = 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: 8
The sum of numbers 5.0 and 8.0 is 13.0

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

Enter first number: 56
Enter second number: 98
The sum of numbers 56.0 and 98.0 is 154.0

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.

# Python program to add two numbers with user input

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: 12
The sum of numbers 6.0 and 2.0 is 8.0

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

Enter first number: 12
Enter second number: 3.5
The sum of numbers 12.0 and 3.5 is 15.5

Sum of Two Numbers in Python with User Input

This is a different method of adding two numbers in python. Let us see how to add two numbers in python without using any variables. This addition program is written in only one-line statements.

# Python program to add two numbers in one line
# Without using any variables

print('The sum is %.2f' %(float(input('Enter First Number: ')) 
                      + float(input('Enter Second Number: '))))

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

Enter First Number: 25
Enter Second Number: 12
The sum is 37.00

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

Enter First Number: 45
Enter Second Number: 0.3
The sum is 45.30

Python Program to Add Two Numbers with User Input

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 with user input

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: 12
Enter second number: 36
The sum of numbers 12.0 and 36.0 is 48.0

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

Enter first number: 25.63
Enter second number: 5.3
The sum of numbers 25.63 and 5.3 is 30.93

# Python program to add two numbers with user input

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: 98
Enter second number: 5
The sum of numbers 98 and 5 is 103

Leave a Comment

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