2
3
Sample Output:5
input()
function to get two numbers from the user. Ensure the input is of the correct type (integer or float).print()
.+
Operator+
operator.# Program to add two numbers using + operator
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("The sum is:", a + b)
Input:10
20
Output:The sum is: 30
# Program to add two numbers using * and - operators
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
sum = (a * a - b * b) // (a - b)
print("The sum is:", sum)
Input:10
20
Output:The sum is: 30
# Program to add two numbers using if-else
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a != b:
print("The sum is:", a + b)
else:
print("The sum is:", 2 * a)
Input:5
5
Output:The sum is: 10
# Add two numbers using functions
def add(a, b):
return a + b
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("The sum is:", add(a, b))
Input:10
15
Output:The sum is: 25
# Add two numbers using bitwise operators
def add_without_plus(a, b):
while b != 0:
carry = a & b
a = a ^ b
b = carry << 1
return a
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("The sum is:", add_without_plus(a, b))
Input:12
24
Output:The sum is: 36
# Add two floating-point numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print(f"The sum of {num1} and {num2} is {sum}")
Input:15.5
26.4
Output:The sum of 15.5 and 26.4 is 41.9
# Add two numbers in one line
print("The sum is", float(input("Enter the first number: ")) + float(input("Enter the second number: ")))
Input:67
21
Output:The sum is 88.0