You are on page 1of 2

Practical 2

print("Name: Ansh Sanjay Agrawal")


print("Practical-2")
print("Roll no- 20")
print("Semester- 3")
print("Batch- A2")

Name: Ansh Sanjay Agrawal


Practical-2
Roll no- 20
Semester- 3
Batch- A2

Aim: To design a calculator using function and class.

class Calculator:
def addition(self):
a = int(input("Enter first number:"))
b = int(input("Enter Second number:"))
print(a + b)
def subtraction(self):
a = int(input("Enter first number:"))
b = int(input("Enter Second number:"))
print(a - b)
def multiplication(self):
a = int(input("Enter first number:"))
b = int(input("Enter Second number:"))
print(a * b)
def division(self):
a = int(input("Enter first number:"))
b = int(input("Enter Second number:"))
print(a / b)
obj = Calculator()
choice = 1
while choice != 0:
print("1. ADDITION")
print("2. SUBTRACTION")
print("3. MULTIPLICATION")
print("4. DIVISION")
print("5. EXIT")
choice = int(input("Enter your choice:"))
if choice == 1:
print(obj.addition())
elif choice == 2:
print(obj.subtraction())
elif choice == 3:
print(obj.multiplication())
elif choice == 4:
print(obj.division())
elif choice == 5:
break
else:
print("Invalid choice")

1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice:1
Enter first number:20
Enter Second number:30
50
None
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
Enter your choice:5

Write a python function to find a factorial of given number

num = int(input("Enter a number: "))


factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Enter a number: 8
The factorial of 8 is 40320

Create a Python program that check if given number is even or odd

num = int(input("Enter a number: "))


if num %2 ==0:
print("the number is even")
else:
print("the number is odd")

Enter a number: 16
the number is even

You might also like