A program to check if a number is positive, negative, odd, even, zero

Python Methods and Functions

Check if a number is positive, negative, odd, even, or zero. This problem is solved with the if… elif… else statement and the nested if… else statement. 
Suitable:

  • The number is positive if it is greater than zero. We check this in the if statement.
  • If the value is False, the number will be either zero or negative.
  • This is also checked in the following statement.
  • In case odd and even number, a number is even if it is perfectly divisible by 2.
    • When a number is divisible by 2, we use the remainder operator% to calculate the remainder.
    • If the remainder is not zero, the number is odd.

Examples:

 Input: 10 Output: Positive number 10 is Even 
 Input: 0 Output: 0 is Even 

# Python Code to check if a number is
# Positive, Negative, Odd, Even, Zero
# Using if ... elif ... else

num = 10

if num & gt;  0 :

print ( "Positive number" )

elif num = = 0 :

print ( "Zero" )

else :

print ( "Negative number" )

 
# Checking for odd and even

if (num % 2 ) = = 0 :

print ( " {0} is Even " . format (num))

else :

print ( "{0} is Odd" . format (num))

 Output: Positive number 10 is Even 

# Python Code to check if a number is
# Positive, Negative, Odd, Even , Zero
# Using Nested if

num = 20

if num & gt; = 0 :

if num = = 0 :

print ( "Zero " )

  else : < / code>

print ( "Positive number" )

else :

print ( " Negative number " )

  
# Odd and even check

if (num % 2 ) = = 0 :

print ( "{0} is Even" . format (num))

else :

print ( "{0} is Odd" . format (num))

 Output: Positive number 20 is Even 




Tutorials