Thursday, October 17, 2024

30 Python Programs Collections – 02 - SELECTION STRUCTURE - IF...ELIF....ELSE

 




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.")

50 Python Programs Collections – 01 - SEE COMPUTER SCIENCE

 



1.     Sum of two numbers

 

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

sum = num1 + num2

print("Sum of two numbers:", sum)

 

2.     Difference of two numbers

 

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

difference = num1 - num2

print("Difference of two numbers:", difference)

 

3.     Product of two numbers

 

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

product = num1 * num2

print("Product of two numbers:", product)

 

4.     Division of two numbers

 

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

division = num1 / num2

print("Division of two numbers:", division)

 

5.     Modulus of two numbers

 

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

remainder = num1 % num2

print("Remainder of two numbers:", remainder)

 

6.     Find the average of two numbers

 

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

average = (num1 + num2) / 2

print("Average of two numbers:", average)

 

7.     Sum, product, and average of three numbers

 

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

sum = num1 + num2 + num3

product = num1 * num2 * num3

average = sum_of_numbers / 3

print(f"Sum of three numbers: {sum }")

print(f"Product of three numbers: {product }")

print(f"Average of three numbers: {average }")

8.     Find the square of a number

 

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

square = num ** 2

print("Square of the number:", square)

 

9.     Find the cube of a number

 

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

cube = num ** 3

print("Cube of the number:", cube)

 

10.  Area of a rectangle

 

length = float(input("Enter the length of the rectangle: "))

breadth = float(input("Enter the breadth of the rectangle: "))

area = length * breadth

print("Area of the rectangle:", area)

 

11.  Perimeter of a rectangle

 

length = float(input("Enter the length of the rectangle: "))

breadth = float(input("Enter the breadth of the rectangle: "))

perimeter = 2 * (length + breadth)

print("Perimeter of the rectangle:", perimeter)

 

12.  Area of a circle

 

radius = float(input("Enter the radius of the circle: "))

area = 3.14159 * radius ** 2

print("Area of the circle:", area)

 

13.  Circumference of a circle

 

radius = float(input("Enter the radius of the circle: "))

c = 2 * 3.14159 * radius

print("Circumference of the circle:", c)

 

14.  Simple Interest Calculation

 

principal = float(input("Enter the principal amount: "))

rate = float(input("Enter the rate of interest: "))

time = float(input("Enter the time period in years: "))

simple_interest = (principal * rate * time) / 100

print("Simple Interest:", simple_interest)

 

15.  Calculate the total price after tax

 

price = float(input("Enter the price of the item: "))

tax_rate = float(input("Enter the tax rate: "))

total_price = price + (price * tax_rate / 100)

print("Total price after tax:", total_price)

 

16.  Calculate the volume of a cube

 

side = float(input("Enter the side length of the cube: "))

volume = side ** 3

print("Volume of the cube:", volume)

 

17.  Calculate the area of a square

 

side = float(input("Enter the side length of the square: "))

area = side ** 2

print("Area of the square:", perimeter)

 

18.  Calculate the perimeter of a square

 

side = float(input("Enter the side length of the square: "))

perimeter = 4 * side

print("Perimeter of the square:", perimeter)

 

19.  Calculate the area of a triangle

 

base = float(input("Enter the base of the triangle: "))

height = float(input("Enter the height of the triangle: "))

area = 0.5 * base * height

print("Area of the triangle:", area)

 

20.  Calculate the surface area of a sphere

 

radius = float(input("Enter the radius of the sphere: "))

surface_area = 4 * 3.14159 * (radius ** 2)

print("Surface area of the sphere:", surface_area)

 

21.  Calculate the surface area of a cube

 

side = float(input("Enter the side length of the cube: "))

surface_area = 6 * (side ** 2)

print("Surface area of the cube:", surface_area)

 

22.  Calculate the volume of a cylinder

 

radius = float(input("Enter the radius of the cylinder: "))

height = float(input("Enter the height of the cylinder: "))

volume = 3.14159 * radius ** 2 * height

print("Volume of the cylinder:", volume)

 

23.  Calculate the volume of a cone

 

radius = float(input("Enter the radius of the cone: "))

height = float(input("Enter the height of the cone: "))

volume = (1/3) * 3.14159 * (radius ** 2) * height

print("Volume of the cone:", volume)

 

 

 

24.  Total cost for multiple items

 

price_per_item = float(input("Enter the price per item: "))

quantity = int(input("Enter the quantity of items: "))

total_cost = price_per_item * quantity

print("Total cost of item:", total_cost)

 

25.  Calculate the perimeter of a triangle

 

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: "))

perimeter = side1 + side2 + side3

print("Perimeter of the triangle:", perimeter)

 

26.  Calculate the area of a parallelogram

 

base = float(input("Enter the base of the parallelogram: "))

height = float(input("Enter the height of the parallelogram: "))

area = base * height

print("Area of the parallelogram:", area)

 

27.  Convert Celsius to Fahrenheit

 

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9/5) + 32

print("Temperature in Fahrenheit:", fahrenheit)

 

28.  Convert Fahrenheit to Celsius

 

fahrenheit = float(input("Enter temperature in Fahrenheit: "))

celsius = (fahrenheit - 32) * 5/9

print("Temperature in Celsius:", celsius)

 

29.  Convert hours to minutes

 

hours = float(input("Enter the number of hours: "))

minutes = hours * 60

print("Minutes:", minutes)

 

30.  Convert minutes to hours

 

minutes = float(input("Enter the number of minutes: "))

hours = minutes / 60

print("Equivalent hours:", hours)

 

31.  Convert minutes to seconds

 

minutes = float(input("Enter the number of minutes: "))

seconds = minutes * 60

print("Equivalent seconds:", seconds)

 

 

32.  Convert seconds to hours

 

seconds = float(input("Enter the number of seconds: "))

hours = seconds / 3600

print("Equivalent hours:", hours)

 

33.  Convert days to hours

 

days = float(input("Enter the number of days: "))

hours = days * 24

print("Hours:", hours)

 

34.  Convert hours to days

 

hours = float(input("Enter the number of hours: "))

days = hours / 24

print("Equivalent days:", days)

 

35.  Convert kilometers to miles

 

kilometers = float(input("Enter distance in kilometers: "))

miles = kilometers * 0.621371

print("Distance in miles:", miles)

 

36.  Convert kilometers to meters

 

kilometers = float(input("Enter distance in kilometers: "))

meters = kilometers * 1000

print("Distance in meters:", meters)

 

37.  Convert meters to centimeters

 

meters = float(input("Enter distance in meters: "))

centimeters = meters * 100

print("Distance in centimeters:", centimeters)

 

38.  Convert grams to kilograms

 

grams = float(input("Enter weight in grams: "))

kilograms = grams / 1000

print("Weight in kilograms:", kilograms)

 

39.  Convert kilograms to pounds

 

kilograms = float(input("Enter weight in kilograms: "))

pounds = kilograms * 2.20462

print("Weight in pounds:", pounds)

 

40.  Convert liters to milliliters

 

liters = float(input("Enter volume in liters: "))

milliliters = liters * 1000

print("Volume in milliliters:", milliliters)

41.  Convert milliliters to liters

 

milliliters = float(input("Enter volume in milliliters: "))

liters = milliliters / 1000

print("Volume in liters:", liters)

 

42.  Convert feet to inches

 

feet = float(input("Enter height in feet: "))

inches = feet * 12

print("Height in inches:", inches)

 

43.  Convert inches to feet

 

inches = float(input("Enter height in inches: "))

feet = inches / 12

print("Height in feet:", feet)

 

44.  Convert kilograms to grams

 

kilograms = float(input("Enter weight in kilograms: "))

grams = kilograms * 1000

print("Weight in grams:", grams)

 

45.  Write a Python program to solve a quadratic equation of the form ax^2 + bx + c = 0 using the quadratic formula.

 

import math

 

a = float(input("Enter the coefficient a: "))

b = float(input("Enter the coefficient b: "))

c = float(input("Enter the coefficient c: "))

discriminant = b**2 - 4*a*c

root1 = (-b + math.sqrt(discriminant)) / (2*a)

root2 = (-b - math.sqrt(discriminant)) / (2*a)

print(f"The roots of the quadratic equation are: {root1} and {root2}")

 

46.  Python program to convert USD (U.S. Dollars) to Nepali Currency (NPR)

 

usd = float(input("Enter the amount in USD: "))

conversion_rate = 132.52

npr = usd * conversion_rate

print(f"{usd} USD is equal to {npr} NPR")

 

47.  Python program to convert Nepali Currency (NPR) to Indian Currency (INR)

 

npr = float(input("Enter the amount in Nepali Currency (NPR): "))

conversion_rate = 0.625

inr = npr * conversion_rate

print(f"{npr} NPR is equal to {inr} INR")

 

 

 

48.  Python program that takes an input in seconds and converts it into hours, minutes, and seconds

 

total_seconds = int(input("Enter the number of seconds: "))

hours = total_seconds // 3600

minutes = (total_seconds % 3600) // 60

seconds = total_seconds % 60

print(f"{total_seconds} seconds is equal to {hours} hours, {minutes} minutes, and {seconds} seconds.")

 

49.  Python program that takes an input in days and converts it into years, months, and days

 

total_days = int(input("Enter the number of days: "))

years = total_days // 365

remaining_days = total_days % 365

months = remaining_days // 30

days = remaining_days % 30

print(f"{total_days} days is equal to {years} years, {months} months, and {days} days.")

 

50.  Python program to calculate the volume and total surface area of a hemisphere

 

import math

radius = float(input("Enter the radius of the hemisphere: "))

volume = (2/3) * math.pi * radius**3

surface_area = 3 * math.pi * radius**2

print(f"Volume of the hemisphere: {volume} cubic units")

print(f"Total surface area of the hemisphere: {surface_area} square units")