Practical 1
Objective: Write a program to check whether a number is positive or negative.
Code:
num = float(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Output:
Practical 2
Objective: Write a program to check whether a number is even or odd.
Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output:
Practical 3
Objective: Write a program to check if a number is divisible by 5 and 11.
Code:
num = int(input("Enter a number: "))
if num % 5 == 0 and num % 11 == 0:
print("The number is divisible by both 5 and 11.")
else:
print("The number is not divisible by both 5 and 11.")
Output:
Practical 4
Objective: Write a program to check whether a number is divisible by 3 or 7.
Code:
num = int(input("Enter a number: "))
if num % 3 == 0 or num % 7 == 0:
print("The number is divisible by either 3 or 7.")
else:
print("The number is n
ot divisible by either 3 or 7.")
Output:
Practical 5
Objective: Input three sides and check: (using nested if)
● If triangle is valid
● Then determine whether it is:
○ Equilateral
○ Isosceles
○ Scalene
Code:
side1 = float(input("Enter the length of the first side: "))
side2 = float(input("Enter the length of the second side: "))
side3 = float(input("Enter the length of the third side: "))
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 +
side3 > side1):
print("The triangle is valid.")
if side1 == side2 == side3:
print("The triangle is equilateral.")
elif side1 == side2 or side1 == side3 or side2 == side3:
print("The triangle is isosceles.")
else:
print("The triangle is scalene.")
else:
print("The triangle is not valid.")
Output:
Practical 6
Objective: Write a program to check whether a number is pallindrome or armstrong.
Code:
num = int(input("Enter a number: "))
# Check for palindrome
num_str = str(num)
if num_str == num_str[::-1]:
print("The number is a palindrome.")
else:
print("The number is not a palindrome.")
# Check for armstrong
num_digits = len(num_str)
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
if armstrong_sum == num:
print("The number is an armstrong number.")
else:
print("The number is not an armstrong number.")
Output:
Practical 7
Objective: Write a program to calculate the area and perimeter of rectangle.
Code:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
perimeter = 2 * (length + width)
print("The area of the rectangle is:", area)
print("The perimeter of the rectangle is:", perimeter)
Output:
Practical 8
Objective: Print numbers from 1 to 10 using a loop.
Code:
for i in range(1, 11):
print(i)
Output:
Practical 9
Objective: Print numbers from 10 to 1 (reverse order).
Code:
for i in range(10, 0, -1):
print(i)
Output:
Practical 10
Objective: Print even numbers from 1 to 20.
Code:
for i in range(1, 21):
if i % 2 == 0:
print(i)
Output:
Practical 11
Objective: Print odd numbers from 1 to 20.
Code:
for i in range(1, 21):
if i % 2 != 0:
print(i)
Output:
Practical 12
Objective: Print numbers between 1–50 divisible by 3.
Code:
for i in range(1, 51):
if i % 3 == 0:
print(i)
Output:
Practical 13
Q13. Print Fibonacci series up to n terms.
Code:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci series up to", n, "terms:")
for _ in range(n):
print(a)
a, b = b, a + b
Output:
Practical 14
Objective: ATM system:
● Ask PIN at least once
● Stop after correct PIN or 3 attempts
Code:
correct_pin = "1234"
attempts = 0
while attempts < 3:
entered_pin = input("Enter your PIN: ")
if entered_pin == correct_pin:
print("PIN accepted. Access granted.")
break
else:
attempts += 1
print("Incorrect PIN. Try again.")
if attempts == 3:
print("Too many incorrect attempts. Access denied.")
Output:
Practical 15
Objective: Menu-driven calculator:
● Perform operation once
● Ask whether to continue16. Write a program to print factorial of n numbers.
Code:
def calculator():
while True:
print("Menu:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '5':
print("Exiting the calculator.")
break
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if choice == '1':
result = num1 + num2
print(f"The result of {num1} + {num2} is: {result}")
elif choice == '2':
result = num1 - num2
print(f"The result of {num1} - {num2} is: {result}")
elif choice == '3':
result = num1 * num2
print(f"The result of {num1} * {num2} is: {result}")
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(f"The result of {num1} / {num2} is: {result}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid choice. Please select a valid option.")
calculator()
Output: