Addition of Two Numbers in Python

Write a Program to Addition of Two Numbers

Python Examples-InterviewGIGHere’s a program written in Python to perform the addition of two numbers, along with an explanation and the expected output:

Program:

# Taking input from the user--comments
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2    # Adding the two numbers
print("Sum:", sum)  # Displaying the result

Explanation:

  • The program starts by prompting the user to enter two numbers using the input() function. The float() function is used to convert the input into floating-point numbers, allowing decimal values.
  • The numbers are stored in the variables num1 and num2.
  • The + operator is used to add the values of num1 and num2, and the result is stored in the variable sum.
  • The print() function is used to display the sum with a descriptive message.

Output:

Enter first number: 6
Enter second number: 8
Sum: 14.0

In this example, if the user enters the numbers 6 and 8, the program calculates their sum (6 + 8 = 14) and displays the result as “Sum: 14.0“. The result is displayed as a floating-point number because we used the float() function when taking input.

Scroll to Top