Python Program Collection 03
1. Check if a number is positive or negative
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.")
2. Check if a number is positive, negative, or zero
num = float(input("Enter a number: "))
if num > 0:
print(f"{num} is positive.")
elif num < 0:
print(f"{num} is negative.")
else:
print(f"{num} is zero.")
3. Check if a number is positive, negative, or zero using ternary operator
num = float(input("Enter a number: "))
print("Positive" if num > 0 else "Negative" if num < 0 else "Zero")
4. Check if a number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even.")
else:
print(f"{num} is odd.")
5. Find the largest of two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if num1 > num2:
print(f"The largest number is {num1}")
elif num2 > num1:
print(f"The largest number is {num2}")
else:
print("Both numbers are equal.")
6. Determine the minimum of three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if num1 <= num2 and num1 <= num3:
print(f"The smallest number is {num1}.")
elif num2 <= num1 and num2 <= num3:
print(f"The smallest number is {num2}.")
else:
print(f"The smallest number is {num3}.")
7. Check if a year is a leap year
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
8. Grade Calculation
marks = float(input("Enter your marks: "))
if marks >= 90:
grade = 'A'
elif marks >= 80:
grade = 'B'
elif marks >= 70:
grade = 'C'
elif marks >= 60:
grade = 'D'
else:
grade = 'F'
print(f"Your grade is {grade}.")
9. Check if a person is eligible to vote
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
10. Check if a number is divisible by 5 and 11
num = int(input("Enter a number: "))
if num % 5 == 0 and num % 11 == 0:
print(f"{num} is divisible by both 5 and 11.")
else:
print(f"{num} is not divisible by both 5 and 11.")
11. Check if a number is divisible by both 3 and 9
num = int(input("Enter a number: "))
if num % 3 == 0 and num % 9 == 0:
print(f"{num} is divisible by both 3 and 9.")
else:
print(f"{num} is not divisible by both 3 and 9.")
12. Check the type of triangle
a = float(input("Enter the first side: "))
b = float(input("Enter the second side: "))
c = float(input("Enter the third side: "))
if a == b == c:
print("The triangle is equilateral.")
elif a == b or b == c or a == c:
print("The triangle is isosceles.")
else:
print("The triangle is scalene.")
13. Check if a number is a multiple of 3 or 7
num = int(input("Enter a number: "))
if num % 3 == 0:
print(f"{num} is a multiple of 3.")
elif num % 7 == 0:
print(f"{num} is a multiple of 7.")
else:
print(f"{num} is not a multiple of 3 or 7.")
14. Check if a person can drive based on age
age = int(input("Enter your age: "))
if age >= 18:
print("You are allowed to drive.")
else:
print("You are not allowed to drive.")
15. Check if a triangle is valid
a = float(input("Enter the first side: "))
b = float(input("Enter the second side: "))
c = float(input("Enter the third side: "))
if a + b > c and a + c > b and b + c > a:
print("The triangle is valid.")
else:
print("The triangle is not valid.")
16. Check if a triangle is right-angled
a = float(input("Enter the first side: "))
b = float(input("Enter the second side: "))
c = float(input("Enter the third side: "))
if a**2 + b**2 == c**2 or a**2 + c**2 == b**2 or b**2 + c**2 == a**2:
print("The triangle is right-angled.")
else:
print("The triangle is not right-angled.")
17. Check if a person is a child, teenager, or adult
age = int(input("Enter your age: "))
if age < 12:
print("You are a child.")
elif 12 <= age < 18:
print("You are a teenager.")
else:
print("You are an adult.")
18. Check if a given temperature is freezing, normal, or hot
temp = float(input("Enter the temperature in Celsius: "))
if temp <= 0:
print("The weather is freezing.")
elif temp < 25:
print("The weather is normal.")
else:
print("The weather is hot.")
19. Check if a number is a perfect square
import math
num = int(input("Enter a number: "))
sqrt = math.isqrt(num)
if sqrt * sqrt == num:
print(f"{num} is a perfect square.")
else:
print(f"{num} is not a perfect square.")
20. Check if a number is a cube number
num = int(input("Enter a number: "))
cube_root = round(num ** (1/3))
if cube_root ** 3 == num:
print(f"{num} is a cube number.")
else:
print(f"{num} is not a cube number.")
No comments:
Post a Comment