Python program to use arithmetic operators for carrying out simple operations

In this example, we will write a simple program that takes input from the user and perform a basic arithmetic operation in Python. To better understand this example, make sure you have knowledge of the following tutorials:-

Source Code:

a = float(input("Input first number: "))
b = float(input("Input second number: "))

c = a+b  # addition
d = a*b  # multiplication
e = a/b  # division
f = a-b  # subtraction
g = b % a  # remainder
h = b ** a # exponential

print("sum = {addition}\n"
      "subtraction = {subtraction}\n"
      "product = {product}\n"
      "division = {division}\n"
      "Modulus = {modulus}\n"
      "Exponential = {exponent}" .
      format(addition=c, product=d, division=e,subtraction=f,modulus=g,exponent=h))

The output of the above program is:-

Input first number: 15
Input second number: 6
sum = 21.0
subtraction = 9.0
product = 90.0
division = 2.5
Modulus = 6.0
Exponential = 470184984576.0
In this program, the user input is taken using input() function which takes input as a string. But as we are doing the calculation part, we need the input to be in float. So, we did the type-conversion using float() function.
The input from the user is taken as a and b and basic operations are performed and result id displayed using print with the formatting of the data.
SHARE Python program to use arithmetic operators for carrying out simple operations

You may also like...

Leave a Reply

Your email address will not be published.

Share