Python Program to Check if a Number is Positive, Negative or 0

226
views
Program to Check if a Number is Positive, Negative or 0

In this example, you will learn to check whether the user entered Number is Positive, Negative or 0

This Program to find Number is Positive, Negative or 0 can be solved using if…elif…else and nested if…else statement.

To understand this example, you should have the knowledge of following Python programming topics:

  1. Python if…else Statement
  2. Python Input, Output and Import

Program to Check if a Number is Positive, Negative or 0 using if…elif…else

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

Output

Enter a number: 2
Positive number

Number is Positive, Negative or 0 using if…elif…else

Here, we have used the if...elif...else statement. We can do the same thing using nested if statements as follows.

Program to Check if a Number is Positive, Negative or 0 using Using Nested if

num = float(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")

Output

Enter a number: 2
Positive number

Number is Positive, Negative or 0 using Nested if

If you notice the output of both the Programs is the same, so we can use any method to Check if a Number is Positive, Negative or zero.

Ask your questions and clarify your/others doubts on Program to Check if a Number is Positive, Negative or zero by commenting. Python Documentation

Related Program

  1. Python Program to Find LCM
  2. Python Program to Find HCF or GCD
  3. Python Program To Display Powers of 2 Using Anonymous Function
  4. Python Program to Display Calendar