Given two numbers num1 and num2. Write a Python program to perform the addition of given two numbers and print the output sum.
Example: Given: num1 = 10, num2 = 20 Output: 30 Given: num1 = 5, num2 = 8 Output: 13
Example Python Code:
# take input from the user num1 = input("Enter a number: ") num2 = input("Enter another number: ") # add the two numbers sum = float(num1) + float(num2) # print the result print("The sum of {0} and {1} is {2}".format(num1, num2, sum))
Enter a number: 12
Enter another number: 41
The sum of 12 and 41 is 53.0
Explanation:
This program takes two numbers as input from the user using the input() function, which returns the input as a string. The input is then converted to a float using the float() function so that the numbers can be added together.
The result is stored in the variable sum, which is then printed out using the print() function. The format() method places the values of num1, num2, and sum into the string.
No comments:
Post a Comment