Write a program to enter two integers and perform all arithmetic operations on them Python

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.0

How 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 + b

We perform the addition of two numbers with the help of the plus (+) operator.

# Subtraction of two numbers difference = a - b

We perform subtraction of two numbers using the minus (-) operator.

# Multiplication of two numbers multiplication = a * b

Multiplication of two numbers is done with the help of (*) operator.

# Division of two numbers division = a / b

Similarly, division is done with the help of (/) operator.

# Modulus of two numbers mod = a % b

Modulus is obtained using the (%) operator.

# Exponent exp = a ** b

Exponent 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.

Arithmetic Operations — programminginpython.com

Task :

To perform arithmetic operations on two integers.

Approach :

  • Read two input integers using input() or raw_input().
  • Addition operation using + operator, num1 + num2 adds 2 numbers.
  • Subtraction operation using - operator, num1 - num2 right hand operand from left hand operand.
  • Multiplication operation using * operator, num1 * num2 multiplies 2 numbers.
  • Division operation using / operator, num1 / num2 divides left hand operand by right hand operand.
  • Floor Division operation using // operator, num1 // num2 divides left hand operand by right hand operand, here it removes the values after decimal point.
  • Modulus % operator when applied returns the remainder when left hand operand is divided by right hand operand num1 % num2.
  • Exponential operation using ** operator, num1 ** num2 returns value of num1 num2
  • Print the result of each operation.

Program :

Output :

Arithmetic Operations — programminginpython.com

>

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


Suggest Corrections

0

Postingan terbaru

LIHAT SEMUA