num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
#source code --> clcoding.com
Code Explanation
num = int(input("Enter a number: "))
The program asks the user to enter a number using input().
input() takes user input as a string, so we use int() to convert it into an integer.
The entered number is stored in the variable num.
if num % 2 == 0:
print("The number is even.")
This if statement checks if the number is divisible by 2 using the modulus operator %.
The modulus operator % returns the remainder after division.
If num % 2 == 0, it means the number is completely divisible by 2, so it is even.
If this condition is true, it prints:
The number is even.
else:
print("The number is odd.")
If the if condition is false, that means num is not divisible by 2.
This means the number is odd, so it prints:
The number is odd.
0 Comments:
Post a Comment