0% found this document useful (0 votes)
9 views5 pages

If Else Problems

The document outlines a series of programming tasks that include checking age groups, determining even or odd numbers, finding the largest number among three inputs, calculating grades based on scores, checking for leap years, and more. Each task is accompanied by example input/output and corresponding solution code in Python. The tasks are designed to practice basic programming concepts and logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

If Else Problems

The document outlines a series of programming tasks that include checking age groups, determining even or odd numbers, finding the largest number among three inputs, calculating grades based on scores, checking for leap years, and more. Each task is accompanied by example input/output and corresponding solution code in Python. The tasks are designed to practice basic programming concepts and logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Check Age Group

Write a program that asks for a person's age and determines which age group they belong to:

 Child (0-12 years)


 Teen (13-19 years)
 Adult (20+ years)

Example Input/Output:

Enter your age: 15


You are a Teen.

2. Even or Odd

Write a program that checks if a number entered by the user is even or odd.

Example Input/Output:

Enter a number: 7
7 is odd.

3. Find the Largest Number

Write a program that takes three numbers from the user and determines the largest.

Example Input/Output:

Enter first number: 5


Enter second number: 8
Enter third number: 3
The largest number is: 8

4. Grade Calculator

Write a program that takes a test score (0-100) as input and outputs the corresponding grade:

 90-100: A
 80-89: B
 70-79: C
 60-69: D
 Below 60: F

Example Input/Output:

Enter your score: 85


Your grade is: B

5. Leap Year Check

Write a program to check if a given year is a leap year. A year is a leap year if:
 It is divisible by 4 and not divisible by 100, or
 It is divisible by 400.

Example Input/Output:

Enter a year: 2024


2024 is a leap year.

6. Positive, Negative, or Zero

Write a program that takes a number as input and checks whether it is positive, negative, or zero.

Example Input/Output:

Enter a number: -10


The number is negative.

7. Check Vowel or Consonant

Write a program that takes a single letter from the user and checks if it is a vowel ( a, e, i, o, u) or a
consonant.

Example Input/Output:

Enter a letter: a
a is a vowel.

8. Triangle Validity

Write a program to check if three given sides can form a triangle. A triangle is valid if the sum of any two
sides is greater than the third.

Example Input/Output:

Enter side 1: 3
Enter side 2: 4
Enter side 3: 5
The sides form a triangle.

9. Eligible to Vote

Write a program that asks for the user's age and checks if they are eligible to vote (18 years or older).

Example Input/Output:

Enter your age: 16


You are not eligible to vote.

10. Shopping Discount

Write a program that calculates the discount based on the total purchase amount:
 No discount if the total is less than $50.
 10% discount if the total is between $50 and $100.
 20% discount if the total is more than $100.

Example Input/Output:

Enter total purchase amount: 120


You get a 20% discount. Final price: $96.0

Challenge Problem: Simple Calculator

Write a program that takes two numbers and an operator (+, -, *, /) as input, then performs the
corresponding operation.

Example Input/Output:

Enter first number: 10


Enter second number: 5
Enter operator (+, -, *, /): *
The result is: 50

SOLUTIONS

1. Check Age Group


age = int(input("Enter your age: "))

if 0 <= age <= 12:


print("You are a Child.")
elif 13 <= age <= 19:
print("You are a Teen.")
elif age >= 20:
print("You are an Adult.")
else:
print("Invalid age.")

2. Even or Odd
number = int(input("Enter a number: "))

if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")

3. Find the Largest Number


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

if num1 >= num2 and num1 >= num3:


print(f"The largest number is: {num1}")
elif num2 >= num1 and num2 >= num3:
print(f"The largest number is: {num2}")
else:
print(f"The largest number is: {num3}")
4. Grade Calculator
score = float(input("Enter your score: "))

if 90 <= score <= 100:


print("Your grade is: A")
elif 80 <= score < 90:
print("Your grade is: B")
elif 70 <= score < 80:
print("Your grade is: C")
elif 60 <= score < 70:
print("Your grade is: D")
elif 0 <= score < 60:
print("Your grade is: F")
else:
print("Invalid score.")

5. Leap Year Check


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

6. Positive, Negative, or Zero


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

if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

7. Check Vowel or Consonant


letter = input("Enter a letter: ").lower()

if letter in 'aeiou':
print(f"{letter} is a vowel.")
elif letter.isalpha():
print(f"{letter} is a consonant.")
else:
print("Invalid input.")

8. Triangle Validity
side1 = float(input("Enter side 1: "))
side2 = float(input("Enter side 2: "))
side3 = float(input("Enter side 3: "))

if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
print("The sides form a triangle.")
else:
print("The sides do not form a triangle.")

9. 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. Shopping Discount


total_purchase = float(input("Enter total purchase amount: "))

if total_purchase < 50:


discount = 0
elif 50 <= total_purchase <= 100:
discount = 0.10 * total_purchase
else:
discount = 0.20 * total_purchase

final_price = total_purchase - discount


print(f"You get a discount of ${discount:.2f}. Final price: ${final_price:.2f}")

Challenge Problem: Simple Calculator


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

if operator == '+':
print(f"The result is: {num1 + num2}")
elif operator == '-':
print(f"The result is: {num1 - num2}")
elif operator == '*':
print(f"The result is: {num1 * num2}")
elif operator == '/':
if num2 != 0:
print(f"The result is: {num1 / num2}")
else:
print("Error: Division by zero.")
else:
print("Invalid operator.")

You might also like