How To Find Sum of Three Numbers in Python

Previously, we had to find the sum of three numbers in python. Here, we will discuss how to find sum of three numbers in python. We will see many methods to add three numbers in python. We will give three numbers num1, num2, and num3. Python programs will add these numbers using various methods.

Add Three Numbers in Python

This is the simplest and easiest way to print the addition program in Python. We will take three numbers while declaring the variables and find the sum of three numbers using the arithmetic operator (+). Then, the sum of numbers will be stored in the sum variable. Finally, it will be displayed on the screen.

Program description:- Write a program to add three numbers in python

# Python program to add three numbers

# take inputs
num1 = 3
num2 = 5
num3 = 9

# add three numbers
sum = num1 + num2 + num3

# displaying the addition result
print('{0} + {1} + {2} = {3}'.format(num1, num2, num3, sum))

Output:-

3 + 5 + 9 = 17

How To Find Sum of Three Numbers in Python

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

Program description:- Write a python program to accept three numbers from the user, add the numbers and display it?

# Python program to add three numbers

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

# add three numbers
sum = num1 + num2 + num3

# displaying the adding result
# value will print in float
print('The sum of numbers {0} + {1} + {2} = {3}'
                  .format(num1, num2, num3, sum))

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

Enter first number: 5
Enter second number: 10
Enter third number: 20
The sum of numbers 5.0 + 10.0 + 20.0 = 35.0

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

Enter first number: 15
Enter second number: 46
Enter third number: 10.3
The sum of numbers 15.0 + 46.0 + 10.3 = 71.3

Python Program to Add Three Numbers Using Function

We can also take the help of a function to add three numbers in python. A function is a block of code that performs a specific task.

Program description:- Write a program to add three numbers using the function in python

# Python program to add three numbers

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

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

# calling function
sum_num = add_num(num1, num2, num3)

# displaying the adding result
print('The sum of numbers {0} + {1} + {2} = {3}'
              .format(num1, num2, num3, sum_num))

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

Enter first number: 56
Enter second number: 32
Enter third number: 78
The sum of numbers 56.0 + 32.0 + 78.0 = 166.0

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

Enter first number: 12.6
Enter second number: 9.8
Enter third number: 25.1
The sum of numbers 12.6 + 9.8 + 25.1 = 47.5

Addition of Three Numbers in Python

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

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

print('The sum is %.2f' %(float(input('Enter first number: ')) 
                     + float(input('Enter second number: ')) 
                   + float(input('Enter third number: '))))

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

Enter first number: 12
Enter second number: 25
Enter third number: 36
The sum is 73.00

Leave a Comment

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