Calculator: Two integers were given to Vikas, and he has to print the calculation as per his choice of the calculator option. Help Vikas to find the calculation operation.
In this problem, we have to enter the two integers and print the calculation operation as per user’s choice.
Calculator options:
- Addition
- Subtraction
- Modulus
- Division
- Multiplication
- Power
Calculator with Python
Input Format:
The first line of input consists of 2 integers values which correspond to first and second numbers (separated by space).
The second line of input consists of integer value which corresponds to the choice of the calculation operation you wants to perform.
Output Format:
A line of output consists of calculation operation.
Subtraction operation output should be always positive.
For the division operation, print the output correct up to two decimal places. If the denominator of the division is 0 (zero), then print “Division not possible”.
Refer the Sample Input Output.
Note:
[All the bold text corresponds to input and rest corresponds to output]
Sample Input and Output 1:
Enter the two numbers:
20 3
1.Addition
2.Subtraction
3.Modulus
4.Division
5.Multiplication
6.Power
Enter your choice:
1
Addition: 23
Sample Input and Output 2:
Enter the two numbers:
8 21
1.Addition
2.Subtraction
3.Modulus
4.Division
5.Multiplication
6.Power
Enter your choice:
9
Wrong choice
Sample Input and Output 3:
Enter the two numbers:
20 0
1.Addition
2.Subtraction
3.Modulus
4.Division
5.Multiplication
6.Power
Enter your choice:
4
Division not possible
Solution
print("Enter the two numbers:")
a, b = map(int, input().split())
print("1.Addition")
print("2.Subtraction")
print("3.Modulus")
print("4.Division")
#sauravhathi
print("5.Multiplication")
print("6.Power")
print("Enter your choice:")
c = int(input())
try:
if c == 1:
print("Addition: ", a+b)
elif c == 2:
# should be positive always
print("Subtraction: ", abs(a-b))
elif c == 3:
print("Modulus: ", a%b)
elif c == 4:
print("Division: {:.2f}".format(a/b))
elif c == 5:
print("Multiplication: ", a*b)
elif c == 6:
print("Power: ", a**b)
else:
print("Wrong choice")
except ZeroDivisionError:
#sauravhathi
print("Division not possible")
except ValueError:
print("Wrong choice")
except Exception as e:
print(e)
Happy Learning – If you require any further information, feel free to contact me.