In this post, we will learn how to perform arithmetic operations using Python Programming language.
This program asks the user to enter two floating point numbers, then it performs the following operations
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Exponent
So, without further ado, let’s begin this tutorial.
# Python Program to Perform Arithmetic Operators a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) # Addition of two numbers sum = a + b # Subtraction of two numbers difference = a - b # Multiplication of two numbers multiplication = a * b # Division of two numbers division = a / b # Modulus of two numbers mod = a % b # Exponent exp = a ** b # Displaying output print("The sum of {0} and {1} is {2}" .format(a, b, sum)) print("The difference of {0} and {1} is {2}" .format(a, b, difference)) print("The multiplication of {0} and {1} is {2}" .format(a, b, multiplication)) print("The division of {0} and {1} is {2}" .format(a, b, division)) print("The modulus of {0} and {1} is {2}" .format(a, b, mod)) print("The exponent value of {0} and {1} is {2}" .format(a, b, exp))Output
Enter first number: 10 Enter second number: 2 The sum of 10.0 and 2.0 is 12.0 The difference of 10.0 and 2.0 is 8.0 The multiplication of 10.0 and 2.0 is 20.0 The division of 10.0 and 2.0 is 5.0 The modulus of 10.0 and 2.0 is 0.0 The exponent value of 10.0 and 2.0 is 100.0How Does This Program Work ?
a = float(input("Enter first number: ")) b = float(input("Enter second number: "))In this program, the user is asked to enter two numbers. The value of these two numbers will get stored in the a and b named variable.
# Addition of two numbers sum = a + bWe perform the addition of two numbers with the help of the plus (+) operator.
# Subtraction of two numbers difference = a - bWe perform subtraction of two numbers using the minus (-) operator.
# Multiplication of two numbers multiplication = a * bMultiplication of two numbers is done with the help of (*) operator.
# Division of two numbers division = a / bSimilarly, division is done with the help of (/) operator.
# Modulus of two numbers mod = a % bModulus is obtained using the (%) operator.
# Exponent exp = a ** bExponent of a number is obtained using (**) operator.
# Displaying output print("The sum of {0} and {1} is {2}" .format(a, b, sum)) print("The difference of {0} and {1} is {2}" .format(a, b, difference)) print("The multiplication of {0} and {1} is {2}" .format(a, b, multiplication)) print("The division of {0} and {1} is {2}" .format(a, b, division)) print("The modulus of {0} and {1} is {2}" .format(a, b, mod)) print("The exponent value of {0} and {1} is {2}" .format(a, b, exp))Finally, we print all the results using the print() function.
Conclusion
I hope after going through this post, you understand how to perform different arithmetic operations using Python Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.
Also Read:
This is basic python program for all beginners in python programming language. It simply takes two integer numbers and performs arithmetic operations like addition, subtraction, multiplication, division, floor division, modulus and exponential(power) on them. To perform arithmetic operations on two integers.Task :
Approach :
Program :
Output :
>
Write a program to enter two integers and perform all arithmetic operations on them.
Solution
Program: #Program to input two numbers and performing all arithmetic operations
#Input first number
num1 = int(input("Enter first number: "))
#Input Second number
num2 = int(input("Enter second number: "))
#Printing the result for all arithmetic operations
print("Results:-")
print("Addition: ",num1+num2)
print("Subtraction: ",num1-num2)
print("Multiplication: ",num1*num2)
print("Division: ",num1/num2)
print("Modulus: ", num1%num2)
print("Floor Division: ",num1//num2)
print("Exponentiation: ",num1 ** num2) OUTPUT:
Enter first number: 8
Enter second number: 3
Results:-
Addition: 11
Subtraction: 5
Multiplication: 24
Division: 2.6666666666666665
Modulus: 2
Floor Division: 2
Exponentiation: 512
Computer Science
Computer Science NCERT Solution 2019
All