Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Python program to addition of two numbers | 4 different Methods

Posted on October 10, 2019October 10, 2019

Table of Contents

  • Python program to the addition of two numbers | 4 different Methods
    • Sum of two numbers – Using starnded method
    • Sum of two numbers – Entered by user
    • Sum of two numbers – using the function
    • Sum of two numbers – using recursion

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 

C++ program to 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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes