Page 175 - Ai_V3.0_c11_flipbook
P. 175
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division,
modulus, exponentiation, and floor division.
Operator Description
+ Adds two operands or unary plus
- Subtracts right operand from left operand or unary minus
* Multiplies two operands
/ Divides left operand by right operand
% Returns the remainder of the division
// Performs floor division (a division that results in whole number adjusted to the left in
the number line)
** Performs exponentiation (left operand raised to the power of right operand)
Program 5: To demonstrate the use of arithmetic operators
#The input() function returns a string
#So we use the int() function to convert the strings to integers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Perform arithmetic operations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
modulus = num1 % num2
exponentiation = num1 ** num2
floor_division = num1 // num2
# Display the results
print(num1, "+", num2, "=", addition)
print(num1, "-", num2, "=", subtraction)
print(num1, "*", num2, "=", multiplication)
print(num1, "/", num2, "=", division)
print(num1, "%", num2, "=", modulus)
print(num1, "**", num2, "=", exponentiation)
print(num1, "//", num2, "=", floor_division)
Output:
Enter the first number: 16
Enter the second number: 3
16 + 3 = 19
Python Programming 173

