0% found this document useful (0 votes)
16 views10 pages

IF Statement Based Programs

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)
16 views10 pages

IF Statement Based Programs

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/ 10

1.

Check if a Number is Positive, Negative, or Zero


● Question: Write a program that checks whether a number is positive, negative, or zero.

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

if num > 0:
print(f"{num} is positive")
if num < 0:
print(f"{num} is negative")
if num == 0:
print("The number is zero")

2. Check if a Number is Divisible by 5


● Question: Write a program that checks if a number is divisible by 5.

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

if num % 5 == 0:
print(f"{num} is divisible by 5")

3. Check if a Character is an Uppercase Letter


● Question: Write a program to check if a given character is an uppercase letter.

char = input("Enter a character: ")

if char.isupper():
print(f"{char} is an uppercase letter")

4. Check if a Number is Within a Specific Range


● Question: Write a program to check if a number is between 10 and 20, inclusive.

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

if 10 <= num <= 20:


print(f"{num} is within the range 10 to 20")
5. Check if a String is a Palindrome
● Question: Write a program to check if a given string is a palindrome (reads the same
forward and backward).

string = input("Enter a string: ")

if string == string[::-1]:
print(f"'{string}' is a palindrome")

6. Check if Two Numbers are Equal


● Question: Write a program to check if two numbers are equal.

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


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

if num1 == num2:
print("Both numbers are equal")

7. Check if a Number is a Multiple of 10


● Question: Write a program to check if a number is a multiple of 10.

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

if num % 10 == 0:
print(f"{num} is a multiple of 10")

8. Check if a Person is Eligible to Vote


● Question: Write a program to check if a person is eligible to vote (age must be 18 or
older).

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

if age >= 18:


print("You are eligible to vote")
9. Check if a String is Empty
● Question: Write a program to check if a given string is empty.

string = input("Enter a string: ")

if len(string) == 0:
print("The string is empty")

10. Check if a Number is Odd


● Question: Write a program to check if a number is odd.

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

if num % 2 != 0:
print(f"{num} is an odd number")

11. Check if a Year is a Leap Year


● Question: Write a program to check if a year is a leap year.

year = int(input("Enter a year: "))

if year % 4 == 0:
if year % 100 != 0 or year % 400 == 0:
print(f"{year} is a leap year")

12. Check if a Number is Prime


● Question: Write a program to check if a number is prime.

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


is_prime = True

if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break

if is_prime:
print(f"{num} is a prime number")

13. Check if a Number is a Perfect Square


● Question: Write a program to check if a number is a perfect square.

import math

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


sqrt = math.sqrt(num)

if sqrt.is_integer():
print(f"{num} is a perfect square")

14. Check if a String Contains a Substring


● Question: Write a program to check if a given string contains a specific substring.

string = input("Enter the main string: ")


substring = input("Enter the substring to check: ")

if substring in string:
print(f"The string '{string}' contains the substring
'{substring}'")

15. Check if a Number is in a List


● Question: Write a program to check if a given number is present in a list.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


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

if num in numbers:
print(f"{num} is in the list")
16. Check if a Triangle is Equilateral
● Question: Write a program to check if a triangle is equilateral (all sides are equal).

side1 = int(input("Enter length of side 1: "))


side2 = int(input("Enter length of side 2: "))
side3 = int(input("Enter length of side 3: "))

if side1 == side2 == side3:


print("The triangle is equilateral")

17. Check if a Number is a Palindrome


● Question: Write a program to check if a number is a palindrome.

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


num_str = str(num)

if num_str == num_str[::-1]:
print(f"{num} is a palindrome")

18. Check if Two Strings are Anagrams


● Question: Write a program to check if two strings are anagrams (contain the same
characters in any order).

str1 = input("Enter first string: ")


str2 = input("Enter second string: ")

if sorted(str1) == sorted(str2):
print(f"'{str1}' and '{str2}' are anagrams")

19. Check if a Number is a Power of 2


● Question: Write a program to check if a number is a power of 2.

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

if num > 0 and (num & (num - 1)) == 0:


print(f"{num} is a power of 2")
20. Check if a String Starts with a Vowel
● Question: Write a program to check if a given string starts with a vowel.

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

if string[0] in 'aeiou':
print(f"The string '{string}' starts with a vowel")

Intermediate Level Programs


1. Check if a Year is a Leap Year

Concept: Using multiple conditions with if-elif-else

Solution:
year = int(input("Enter a year: "))

if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
else:
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")

2. Check if a Number is Divisible by 2, 3, and 5

Concept: if-elif-else for multiple checks

Solution:
num = int(input("Enter a number: "))

if num % 2 == 0 and num % 3 == 0 and num % 5 == 0:


print(f"{num} is divisible by 2, 3, and 5")
else:
print(f"{num} is not divisible by 2, 3, and 5")

3. Determine the Grade Based on Marks

Concept: Nested conditions using if-elif-else

Solution:
marks = int(input("Enter your marks: "))

if marks >= 90:


print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")

4. Check if Three Sides Form a Triangle

Concept: Multiple condition checks using if-else

Solution:

a = int(input("Enter side 1: "))


b = int(input("Enter side 2: "))
c = int(input("Enter side 3: "))

if a + b > c and b + c > a and a + c > b:


print("These sides form a valid triangle")
else:
print("These sides do not form a valid triangle")

5. Check if a Number is a Perfect Square

Concept: if-else with mathematical operations


Solution:

import math

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


sqrt = math.sqrt(num)

if sqrt == int(sqrt):
print(f"{num} is a perfect square")
else:
print(f"{num} is not a perfect square")

Advanced Level Programs


1. Check if a Number is Prime

Concept: if-else with multiple conditions (no loops for simplicity)

Solution:
num = int(input("Enter a number: "))

if num > 1:
if num == 2 or num == 3:
print(f"{num} is a prime number")
elif num % 2 == 0 or num % 3 == 0:
print(f"{num} is not a prime number")
else:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")

2. Classify a Triangle by Its Sides

Concept: if-elif-else for multiple conditions

Solution:
a = int(input("Enter side 1: "))
b = int(input("Enter side 2: "))
c = int(input("Enter side 3: "))
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")

3. Determine the Type of Quadrilateral

Concept: Nested if-elif-else for complex logic

Solution:
a = int(input("Enter side 1: "))
b = int(input("Enter side 2: "))
c = int(input("Enter side 3: "))
d = int(input("Enter side 4: "))

if a == b == c == d:
print("The quadrilateral is a square")
elif a == c and b == d:
print("The quadrilateral is a rectangle")
else:
print("The quadrilateral is some other type")

4. Check if a Point Lies in a Quadrant

Concept: Complex if-elif-else with coordinate checks

Solution:
x = int(input("Enter the x-coordinate: "))
y = int(input("Enter the y-coordinate: "))

if x > 0 and y > 0:


print("The point lies in the first quadrant")
elif x < 0 and y > 0:
print("The point lies in the second quadrant")
elif x < 0 and y < 0:
print("The point lies in the third quadrant")
elif x > 0 and y < 0:
print("The point lies in the fourth quadrant")
else:
print("The point lies on an axis")

5. Solve a Quadratic Equation

Concept: if-else with mathematical operations (discriminant)

Solution:
import math

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


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

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

if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
print(f"The roots are real and different: {root1}, {root2}")
elif discriminant == 0:
root = -b / (2 * a)
print(f"The roots are real and the same: {root}")
else:

○ print("The roots are complex and different")

You might also like