basic

Python program to addition of two numbers | 4 different Methods

Python program to the addition of two numbers | 4 different Methods

In this tutorial, we will discuss the  Python program to the addition of two numbers 

Addition of two numbers

In this post, we are going to learn how to find the sum of two numbers through different 4 ways in Python programming language

Program 1

Sum of two numbers – Using starnded method

#Python program add two integer
num1=34
num2=26

#Find sum of two numbers
sum=num1+num2

#Display the sum of two numbers
print("The sum of {0} and {1} is : {2}:  ".format(num1,num2,sum))

When the above code is executed it produces the following output

The sum of 34 and 26 is : 60:

In this program

  • Integer variables num1,num2 are declared and initialized
  • the num1 and num 2 both are added together  and the value is added to the sum
  • Then, the program is displayed the output (Sum of two integers) using the print() function

Program 2

Sum of two numbers – Entered by user

#Python program add two integer
num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))


#Find sum of two numbers
sum=num1+num2

#Display the sum of two numbers
print("The sum of {0} and {1} is : {2}  ".format(num1,num2,sum))

When the above code is executed it produces the following output

Enter first number: 123
Enter second number: 321
The sum of 123 and 321 is : 444

Approach

  1. Declare variables num1,num2;
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using input() function and stores in the variables num1 and num2 respectively
  5. the num1 and num 2 both are added together  and the value is added to the sum
  6. Then, the program Displays the value of the sum using the print() function

Program 3

Sum of two numbers – using the function

#Python program add two integer

#Find sum of two numbers
def sumNunm(num1,num2):
 sum=num1+num2
 return sum

#Get input from user
num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))

result=sumNunm(num1,num2)

#Display the sum of two numbers
print("The sum of {0} and {1} is : {2}  ".format(num1,num2,result))

When the above code is executed it produces the following output

Enter first number: 21
Enter second number: 39
The sum of 21 and 39 is : 60

Approach

  1. Declare variables num1,num2;
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using input() function and store in the variables num1 and num2 respectively
  5. Create a  function to find the sum
  6. Then, Call the function to produced output
  7. Then, the program displays the value of the sum using the print() function

Program 4

Sum of two numbers – using recursion

#Python program add two integer

#Find sum of two numbers
def sumNum(num1,num2):
  if(num2==0):
     return num1;
  else:
      return(1+sumNum(num1,num2-1));

#Get input from user
num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))

result=sumNum(num1,num2)

#Display the sum of two numbers
print("The sum of {0} and {1} is : {2}:  ".format(num1,num2,result))

When the above code is executed it produces the following output

Enter first number: 124
Enter second number: 346
The sum of 124 and 346 is : 470:

Approach

  1. Declare variables num1,num2;
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using input() function and store in the variables num1 and num2 respectively
  5. Create a recursive function to find the sum
  6. Then, Call the function to produced output
  7. Then, the program Displays the value of the sum using the print() function

 

Similar post

Java program to the addition of two numbers

C++ program to the addition of two numbers

C program to the addition of two numbers

 

Suggested for you

Function in Python

Data type in Python language

The operator in Python language

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

5 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

5 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

6 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

6 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

6 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

6 months ago

This website uses cookies.