Check if a number is positive, negative, or zero
a = float(input('Enter the number: '))
if a >= 0:
if a == 0:
print('The number is zero')
else:
print('The number is a positive number')
else:
print('The number is a negative number')
Output:
Enter the number: 5
The number is a positive number
Calculate the area and perimeter of a parallelogram
b = float(input('Enter the base of parallelogram: '))
w = float(input('Enter the width of parallelogram: '))
h = float(input('Enter the height of parallelogram: '))
Area = b * h
Perimeter = 2 * (b + w)
print('The area of parallelogram is:', Area)
print('The perimeter of parallelogram is:', Perimeter)
Output:
Enter the base of parallelogram: 4
Enter the width of parallelogram: 5
Enter the height of parallelogram: 6
The area of parallelogram is: 24
The perimeter of parallelogram is: 18
Check if a 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")
Output:
Enter a number: 7
The number is odd
Find the factorial of a number
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
Output:
Enter a number: 5
Factorial of 5 is 120
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(year, "is a leap year")
else:
print(year, "is not a leap year")
Output:
Enter a year: 2024
2024 is a leap year
Find the largest among three numbers
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
print("The largest number is", largest)
Output:
Enter first number: 5
Enter second number: 8
Enter third number: 3
The largest number is 8