Table of Contents
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
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
- Declare variables num1,num2;
- The program requires input from the user
- Then the user enters the input value for num1, num2(variables)
- The program will read the input using input() function and stores in the variables num1 and num2 respectively
- the num1 and num 2 both are added together and the value is added to the sum
- 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
- Declare variables num1,num2;
- The program requires input from the user
- Then the user enters the input value for num1, num2(variables)
- The program will read the input using input() function and store in the variables num1 and num2 respectively
- Create a function to find the sum
- Then, Call the function to produced output
- 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
- Declare variables num1,num2;
- The program requires input from the user
- Then the user enters the input value for num1, num2(variables)
- The program will read the input using input() function and store in the variables num1 and num2 respectively
- Create a recursive function to find the sum
- Then, Call the function to produced output
- 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
The operator in Python language