Q2. Python Program to Perform Arithmetic Operations | Solution with Explanation

Hi friends Welcome to DailyCodeHub . Today we are going to learn how to perform all arithmetic operations on two integers in python. This is one of the most commonly asked programs in fresher interviews.

Question : Write a Python program to perform all arithmetic operations on two integers.

Method 1 : Basic operations

Code :


# Assign values to variables
a = 10
b = 3

# Addition
print("Addition:", a + b)

# Subtraction
print("Subtraction:", a - b)

# Multiplication
print("Multiplication:", a * b)

# Division (returns float)
print("Division:", a / b)

# Floor Division (returns integer quotient)
print("Floor Division:", a // b)

# Modulus (remainder)
print("Modulus:", a % b)

# Power (a raised to b)
print("Power:", a ** b)

Output:

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333335

Floor Division: 3

Modulus: 1

Power: 1000


Method 2 : Using Input from User

Code :


# Take input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

# Addition
print("Addition:", a + b)

# Subtraction
print("Subtraction:", a - b)

# Multiplication
print("Multiplication:", a * b)

# Division (returns float)
print("Division:", a / b)

# Floor Division (returns integer quotient)
print("Floor Division:", a // b)

# Modulus (remainder)
print("Modulus:", a % b)

# Power (a raised to b)
print("Power:", a ** b)

Output:

Enter first number: 10

Enter second number: 3

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333335

Floor Division: 3

Modulus: 1

Power: 1000


Method 3 : Using Functions

Code :


# Function for arithmetic operations
def arithmetic(a, b):
    print("Addition:", a + b)
    print("Subtraction:", a - b)
    print("Multiplication:", a * b)
    print("Division:", a / b)
    print("Floor Division:", a // b)
    print("Modulus:", a % b)
    print("Power:", a ** b)
# Call function
arithmetic(10, 3)

Output :

Addition: 13

Subtraction: 7

Multiplication: 30

Division: 3.3333333333333335

Floor Division: 3

Modulus: 1

Power: 1000


Explanation :

Step 1 : We create two variables a = 10 and b = 3 to store two integers.

Step 2 : Addition (+) adds two numbers: 10 + 3 = 13.

Step 3 : Subtraction (- subtracts second number from first 10 - 3 = 7.

Step 4 : Multiplication (*) multiples two numbers. 10 * 3 = 30.

Step 5 : Division (/) always return a float value. 10 / 3 = 3.333

Step 6 : Floor division (/ /) returns only the whole part. 10 // 3 = 3.

Step 7 : Modulus (%) returns the remainder. 10 % 3 = 1.

Step 8 : Power  (**) returns the first number raised to the power of the second. 10 ** 3 = 1000.


Key points :

  • + is uesd for addition
  • - is used for subtraction
  • * is used for multiplication
  • / always returns float even if result is whole number
  • // returns floor value of division
  • % returns remainder of division
  • ** is used for power calculation


I hope this program was helpul . if you have any doubts please leave a comment below. Keep practicing and happy coding

Happy coding ❤️


No comments:

Post a Comment