Site icon Student Projects Live

Check odd or even

Check odd or even

The provided Python program is designed to determine whether a given inputted number is odd or even. It defines a function, check_odd_even, which takes an integer argument and uses the modulo operator to check if the number is divisible by 2. If the remainder is zero, the function returns “Even”; otherwise, it returns “Odd.” The program then prompts the user to input a number, attempts to convert the input to an integer, and calls the check_odd_even function to obtain the result. If the user enters a valid integer, the program prints a message indicating whether the number is odd or even. In case of a non-integer input, it informs the user to enter a valid integer. Overall, the program demonstrates a basic use of functions, user input, and conditional statements in Python for determining the parity of a number.


Source code

def check_odd_even(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"

# Get user input for a number
user_input = input("Enter a number: ")

# Check if the input is a valid integer
try:
    number = int(user_input)
    result = check_odd_even(number)
    print(f"The number {number} is {result}.")
except ValueError:
    print("Please enter a valid integer.")

Description

  1. Function Definition:
  1. User Input:
  1. Input Validation:
  1. Function Invocation:
  1. Output Display:
  1. Execution:

In summary, the program defines a function to check if a number is odd or even, takes user input, validates the input, and then applies the function to determine and display whether the entered number is odd or even.


Screenshot


Download

Exit mobile version