Arithmetic Operations in Python

Arithmetic Operations in Python:

In this blog let us see how we can perform simple arithmetic operations in python.



code:

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2

print("Addition: ", addition)
print("Subtraction: ", subtraction)
print("Multiplication: ", multiplication)
print("Division: ", division)

Output:

Enter first number: 10
Enter second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0

Explanation:

By using this code you can create a simple calculator program that takes two numbers as inputs from the user and performs arithmetic operations (addition, subtraction, multiplication, division) on them.You can also make other arithmetic operations like (Modulus ,Floor Division etc...) by using there appropriate arithmetic operators.


1.Ask the user to enter two numbers:

The program starts by asking the user to enter two number using the 'input()' function. The float() function is used to convert the input values to floating-point numbers.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

2. Perform the four basic arithmetic operations

The program then performs the four basic arithmetic operations (addition, subtraction, multiplication, and division) on the two numbers and stores the results in separate variables.

addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2

3.Print the results to the console

Finally, the program prints the results to the console using the 'print()' function. The results are formatted using string concatenation and the ',' separator to separate the text from the variable values.

print("Addition: ", addition)
print("Subtraction: ", subtraction)
print("Multiplication: ", multiplication)
print("Division: ", division)

Post a Comment

Previous Post Next Post