检查数字是否为正、负、奇、偶、零的程序?


给出数字,我们必须检查数字是偶数还是奇数,而正数还是负数。

算法

Step 1: input number
Step 2: check number is greater than equal to 0 or not. If true then positive otherwise negative and if it 0 then number is 0.
Step 3: if number is divisible by 2 then it’s even otherwise its odd.

示例代码

# Python program check if a number is Positive, Negative, Odd, Even, Zero
n=int(input("Enter Number ::>"))
if n >= 0:
   if n == 0:
      print("The Number Is Zero")
   else:
      print("This Is Positive Number")
   else:
      print("This Is Negative Number")
      # checking for odd and even
   if (n % 2) == 0:
      print("{0} is Even".format(n))
else:
print("{0} is Odd".format(n))

输出

Enter Number ::>20
This Is Positive Number
20 is Even

更新于:2020 年 6 月 23 日

6K+ 浏览量

开启你的 职业生涯

完成课程并获得认证

立即开始
广告