num = int(input("Enter a number: "))
if not (num % 2 == 0):
print("The number is NOT even (it is odd).")
else:
print("The number is even.")
#source code --> clcoding.com
Code Explanation:
Taking Input from the User
num = int(input("Enter a number: "))
The program asks the user to enter a number.
Since input() always returns a string, we use int() to convert it to an integer and store it in the variable num.
Checking if the Number is NOT Even
if not (num % 2 == 0):
num % 2 == 0 checks if num is even:
If num is even, num % 2 == 0 evaluates to True.
If num is odd, num % 2 == 0 evaluates to False.
not (num % 2 == 0) negates the result:
If num is even (True), not True becomes False.
If num is odd (False), not False becomes True.
Printing the Result
print("The number is NOT even (it is odd).")
If the condition not (num % 2 == 0) is True, the program prints:
"The number is NOT even (it is odd)."
else:
print("The number is even.")
If the condition is False (meaning the number is even), the program prints:
"The number is even."
0 Comments:
Post a Comment