Example:
Input: number = 7 Output: 7 is Odd Number Input: number = 10 Output: 10 is Even Number
Algorithm to Check Even or Odd Number.
STEP 1: Accept an integer as input from the user.
STEP 2: Use the modulo operator (%) to check if the number is divisible by 2.
- If the result is 0, the number is even.
- If the result is 1, the number is odd.
STEP 3: Print the result in output.
Python Code to check Even or Odd Numbers:
# Function to check even or odd def check_even_odd(num): if num % 2 == 0: print(f"{num} is an even number.") else: print(f"{num} is an odd number.") # Input number = int(input("Enter an integer: ")) # Check and display result check_even_odd(number)
Enter an integer: 12 12 is an even number.
Explanation:
In the above example, the check_even_odd function takes an integer as an argument and uses the modulo operator to check if it's divisible by 2. If the result is 0, it prints that the number is even; otherwise, it prints that the number is odd.
No comments:
Post a Comment