Curriculum
Course: Python-100-Programmes
Login
Text lesson

C-Conditional Statements-(22 to 34)

C-Conditional Statements

 

22. Check if a number is even or odd.

Input a number and check if it is even or odd

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

 

if num % 2 == 0:

    print(num, “is even”)

else:

    print(num, “is odd”)

 

Example Output:

Enter a number: 7

7 is odd

23. Check if a number is positive, negative, or zero.

Input a number and check if it is positive, negative, or zero

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

 

if num > 0:

    print(num, “is positive”)

elif num < 0:

    print(num, “is negative”)

else:

    print(“The number is zero”)

 

Example Output:

Enter a number: -5

-5.0 is negative

 24. Input a number and check if it is divisible by 3.

Here is a simple Python program to check if a number is divisible by 3:

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

 

if num % 3 == 0:

    print(num, “is divisible by 3”)

else:

    print(num, “is not divisible by 3”)

 

Example Output:

Enter a number: 9

9 is divisible by 3

 

Enter a number: 10

10 is not divisible by 3

25.  Check if a given year is a leap year.

Here is a simple Python program to 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”)

 

Example Output:

Enter a year: 2024

2024 is a leap year

 

Enter a year: 2023

2023 is not a leap year

26.  Input two numbers and display the larger / smaller number.

Here is a simple Python program to display the larger and smaller of two numbers:

a = float(input(“Enter first number: “))

b = float(input(“Enter second number: “))

 

if a > b:

    print(“Larger number:”, a)

    print(“Smaller number:”, b)

elif b > a:

    print(“Larger number:”, b)

    print(“Smaller number:”, a)

else:

    print(“Both numbers are equal:”, a)

 

Example Output 1:

Enter first number: 10

Enter second number: 20

Larger number: 20.0

Smaller number: 10.0

 

Example Output 2:

Enter first number: 15

Enter second number: 15

Both numbers are equal: 15.0

27.Enter three numbers and display the largest/smallest number.

Here is a simple Python program to find the largest and smallest of three numbers:

a = float(input(“Enter first number: “))

b = float(input(“Enter second number: “))

c = float(input(“Enter third number: “))

 

# Finding the largest number

if a >= b and a >= c:

    largest = a

elif b >= a and b >= c:

    largest = b

else:

    largest = c

 

# Finding the smallest number

if a <= b and a <= c:

    smallest = a

elif b <= a and b <= c:

    smallest = b

else:

    smallest = c

 

print(“Largest number:”, largest)

print(“Smallest number:”, smallest)

 

Example Output:

Enter first number: 10

Enter second number: 25

Enter third number: 15

Largest number: 25.0

Smallest number: 10.0

28.Check if a character is a vowel or a consonant.

Here is a simple Python program to check if a character is a vowel or consonant:

char = input(“Enter a single character: “).lower()

 

if char in ‘aeiou’:

    print(char, “is a vowel”)

elif char.isalpha():

    print(char, “is a consonant”)

else:

    print(“Invalid input”)

 

Example Output 1:

Enter a single character: a

a is a vowel

 

Example Output 2:

Enter a single character: b

b is a consonant

 

29.Check if a character is uppercase or lowercase.

 

Here is a simple Python program to check if a character is uppercase or lowercase:

char = input(“Enter a single character: “)

 

if char.isupper():

    print(char, “is uppercase”)

elif char.islower():

    print(char, “is lowercase”)

else:

    print(“Invalid input”)

 

Example Output 1:

Enter a single character: A

A is uppercase

 

Example Output 2:

Enter a single character: g

g is lowercase

 

30.  Check if a character is a digit.

Here is a simple Python program to check if a character is a digit:

char = input(“Enter a single character: “)

 

if char.isdigit():

    print(char, “is a digit”)

elif char.isalpha():

    print(char, “is a letter”)

else:

    print(“Invalid input”)

 

Example Output 1:

Enter a single character: 7

7 is a digit

 

Example Output 2:

Enter a single character: A

A is a letter

31.Input a string and convert the case of characters.

 

Here is a simple Python program to convert the case of characters in a string:

text = input(“Enter a string: “)

 

# Convert uppercase letters to lowercase and vice versa

converted_text = text.swapcase()

 

print(“Converted string:”, converted_text)

 

Example Output 1:

Enter a string: Hello World

Converted string: hELLO wORLD

 

Example Output 2:

Enter a string: PYTHON programming

Converted string: python PROGRAMMING

32.Check if a number is a single-digit, two-digit, three-digit, or more.

Here is a simple Python program to check if a number is single-digit, two-digit, three-digit, or more:

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

 

if 0 <= num <= 9:

    print(num, “is a single-digit number”)

elif 10 <= num <= 99:

    print(num, “is a two-digit number”)

elif 100 <= num <= 999:

    print(num, “is a three-digit number”)

else:

    print(num, “is a number with more than three digits”)

 

Example Output 1:

Enter a number: 7

7 is a single-digit number

 

Example Output 2:

Enter a number: 45

45 is a two-digit number

 

Example Output 3:

Enter a number: 123

123 is a three-digit number

 

Example Output 4:

Enter a number: 1234

1234 is a number with more than three digits

33.Print grade based on marks:

A(80+), B(60-79), C(40-59), D(33-39), F(<33)

Here is a simple Python program to print grade based on marks:

marks = float(input(“Enter marks (out of 100): “))

 

if marks >= 80:

    grade = ‘A’

elif marks >= 60:

    grade = ‘B’

elif marks >= 40:

    grade = ‘C’

elif marks >= 33:

    grade = ‘D’

else:

    grade = ‘F’

 

print(“Grade:”, grade)

 

Example Output 1:

Enter marks (out of 100): 85

Grade: A

 

Example Output 2:

Enter marks (out of 100): 72

Grade: B

 

Example Output 3:

Enter marks (out of 100): 28

Grade: F

34.Check if a number is divisible by 2, 3, and 5.

 

Here’s a Python program that checks if a number is divisible by 2, 3, and 5, and prints which ones it is divisible by:

# Input a number

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

 

# Check divisibility

divisible_by = []

 

if num % 2 == 0:

    divisible_by.append(“2”)

if num % 3 == 0:

    divisible_by.append(“3”)

if num % 5 == 0:

    divisible_by.append(“5”)

 

if divisible_by:

    print(f”{num} is divisible by: {‘, ‘.join(divisible_by)}”)

else:

    print(f”{num} is not divisible by 2, 3, or 5″)

 

Example Output 1:

Enter a number: 30

30 is divisible by: 2, 3, 5

 

Example Output 2:

Enter a number: 14

14 is divisible by: 2

 

Example Output 3:

Enter a number: 7

7 is not divisible by 2, 3, or 5

 

 

 

Scroll to Top