Simple Python Program to Add Two Numbers

In this program, we are going to make a simple python program which will add two numbers with user input.

 In this program, we are going to make a simple python program that will add two numbers with user input. We will take numbers from users or else we can also hard code values.

Pre-requisite: Syntax to print Output in python

I am using f-string to print our result. f-string is a simple print statement that helps us to print variables along with string. We have to just use curly braces to print variables. It starts with the prefix f.
For example:

  
  A=7
  print(f"Number A is {A}")
  
Table of Contents

How to write a program to add two numbers in python with user input

In this example, we take input from the user which is by default of string type so we will typecast them to int. Then we will add and print them.
first=int(input("Enter first Number: "))
second=int(input("Enter Second Number: "))

sum=first+second
print(f"{first} + {second} = {sum}")
Output:

Enter first Number: 56
Enter Second Number: 44
56 + 44 = 100

Simple Python Program to Add Two Numbers

In this example, we will add two numbers in python and print their sum.
a=5
b=10
sum=a+b
print(f"{a} + {b} = {sum}")
Output:

5 + 10 = 15

Program to Add Two numbers without using Addition Operator

first=int(input("Enter first Number: "))
num=first
second=int(input("Enter Second Number: "))
num2=second

while second != 0:
    
    temp = first & second
    first = first ^ second
    second = temp << 1
print(f"{num} + {num2} = {first}")
Output:

Enter first Number: 57
Enter Second Number: 87
57 + 87 = 144

Python Program to Add Two Numbers using function

In this example, we will make a function that will return the addition of two numbers. It takes two numbers in arguments. We can also print the sum of numbers in our function.
def addition(num1,num2):
    return num1+num2

num1=int(input("Enter first Number: "))
num2=int(input("Enter Second Number: "))

print(f"{num1} + {num2} = {addition(num1,num2)}")

Python program to add two numbers using class

class Add:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.sum()

    def sum(self):
        print(f"The sum of {self.a} & {self.b} is {self.a+self.b}")


num1 = int(input("Enter first Number: "))
num2 = int(input("Enter Second Number: "))

num = Add(num1, num2)
Python program to add two numbers using class

About this post:
This is all about the python program to print the sum of two numbers. Please try to make a calculator program in python that provides users with many options like addition, subtraction, multiplication and division. Then the user will choose from these options and your program should print the appropriate result.

If you have any doubts or you are reading till here please comment below your thoughts.

Post a Comment

Please do not enter any spam link in the comment box.