用于检查数字是奇数还是偶数的Python程序

2022-05-03 00:00:00 用于 偶数 奇数

在此示例中,python程序判断输入的数字是奇数还是偶数,程序使用了算术运算符和条件判断

源码

# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))

演示1:

Enter a number: 43
43 is Odd

演示2:

Enter a number: 18
18 is Even

程序运行后要求用户输入一个数字,然后输出该数字是奇数还是偶数。

相关文章