Write a python program to check whether the given number is greater than 50 and also divisible by 2 using nested if else

1. Introduction

This blog post focuses on writing a Python program that checks if a given number is greater than 50 and also divisible by 2. This task is accomplished using nested if-else statements, providing a clear example of basic conditional logic in Python.

2. Program Steps

1. Accept or define a number.

2. Check if the number is greater than 50.

3. If it is, check if it is also divisible by 2.

3. Code Program


def check_number(number):
    # Step 2: Check if the number is greater than 50
    if number > 50:
        # Step 3: Nested if to check divisibility by 2
        if number % 2 == 0:
            print(f"{number} is greater than 50 and divisible by 2.")
        else:
            print(f"{number} is greater than 50 but not divisible by 2.")
    else:
        print(f"{number} is not greater than 50.")

# Step 1: Define a number
num = 64
check_number(num)

Output:

For the number 64, the output will be: 64 is greater than 50 and divisible by 2.

Explanation:

1. The program starts by defining a number, num, which is then passed to the check_number function.

2. Inside the function, the first if statement checks if num is greater than 50. If this condition is true, it proceeds to the nested if statement.

3. The nested if checks if num is divisible by 2 (number % 2 == 0). If this condition is also true, it prints that the number is greater than 50 and divisible by 2. Otherwise, it prints that the number is greater than 50 but not divisible by 2.

4. If the first if condition is not met (i.e., the number is not greater than 50), it prints that the number is not greater than 50.

This program effectively demonstrates the use of nested if-else statements in Python to check multiple conditions sequentially.


Comments