0% found this document useful (0 votes)
11 views

Program 1

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Program 1

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Understanding the Problem


• The program needs to determine whether a given number is even or odd.
• Even numbers are divisible by 2 with no remainder (e.g., 2, 4, 6, etc.).
• Odd numbers have a remainder of 1 when divided by 2 (e.g., 1, 3, 5, etc.).

2. Planning the Solution


• Get input from the user.
• Convert the input to an integer.
• Use the modulus operator (%) to check the remainder when the number is divided
by 2.
• If the remainder is 0, the number is even; otherwise, it’s odd.
• Display the result to the user.

3. Writing the Code


# Step 1: Get input from the user
number = int(input("Enter a number: "))

# Step 2: Check if the number is even or odd


if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")

4. Considering Edge Cases


• What if the user enters a non-numeric input? We might want to add input validation.
• What if the user enters a very large number? We need to ensure that the program
handles large integers.
try:
# Step 1: Get input from the user
number = int(input("Enter a number: "))

# Step 2: Check if the number is even or odd


if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
except ValueError:
print("Please enter a valid integer.")

You might also like