Real Time Example Program for if, else Condition.

 Question 1:

Write a Python program that takes an input number from the user and checks whether the number is divisible by both 3 and 7. If the number is divisible by both, print "The number is divisible by both 3 and 7." Otherwise, print "The number is not divisible by both 3 and 7."



Program:

# Get input for a number and check whether it is divisible by both 3 and 7 or not
a = int(input("Enter a number: "))
if (a % 3 == 0 and a % 7 == 0):
    print("The number is divisible by both 3 and 7")
else:
    print("The number is not divisible by both 3 and 7")


Output Example:

Enter a number: 21
Divisible by 3 and 7

Enter a number: 14
Not Divisible by 3 and 7


Program Step-by-Step Explanation:

Step 1: Input the number

  • The program asks the user to enter a number.

Step 2: Check divisibility

  • It checks if the number is divisible by both 3 and 7.
  • If yes, it prints "Divisible by 3 and 7."
  • Otherwise, it prints "Not Divisible by 3 and 7."




Question 2:

Get input for a number and check if it's odd or even.



Program:

# Get input for a number and check if it's odd
a = int(input("Enter another number: "))
if a % 2 != 0:  # Check if number is odd
    print("Number is odd")
else:
    print("Number is even")


Output Example:

Enter another number: 5
Number is odd

Enter another number: 8
Number is even


Program Step-by-Step Explanation:

Step 1: Input the number

  • The program asks the user to enter another number.

Step 2: Check if the number is odd or even

  • It checks if the number is odd.
  • If the number is odd, it prints "Number is odd."
  • Otherwise, it prints "Number is even."

Post a Comment

0 Comments