write a python program to check whether the number is positive or negative

In the world of programming, determining whether a number is positive or negative is a fundamental task. Python, being a versatile and user-friendly language, provides an easy-to-understand approach to solve this problem. 

In this blog post, we will guide you through the process of creating a Python program that checks whether a number is positive or negative. 

 

write a python program to check whether the number is positive or negative

We will explain how to accept a single number from the user, followed by code explanation, output examples, and a conclusion. So, let's get started!

 

Title: write a python program to check whether the number is positive or negative

 

Accepting a Number from User Input

To begin with, we need to accept a number from the user. Python provides the input() function to receive user input. Let's take a look at the code 



number = float(input("Enter a number: "))

In the above code, the input() function prompts the user to enter a number. The input is then converted to a float using the float() function and stored in the number variable.

 

Checking Whether the Number is Positive or Negative:

Now that we have obtained the number, we can proceed to check whether it is positive or negative. We will use a conditional statement to perform the check. Let's take a look at the code snippet:




if number > 0:
print(f"{number} is positive")
elif number < 0:
print(f"{number} is negative")
else:
print(f"{number} is zero")


In the above code, we use a series of conditional statements to determine whether the number is positive, negative, or zero. We use the print() function to display the result.

 

 

 

Python Complete Program

Now that we have explained the process of accepting a number from the user and checking whether it is positive or negative, let's combine both parts and display the final code:


number = float(input("Enter a number: "))

if number > 0:
print(f"{number} is positive")
elif number < 0:
print(f"{number} is negative")
else:
print(f"{number} is zero")


 

Output 1:



Enter a number: 7.5
7.5 is positive


 

Output 2:



Enter a number: -3.2
-3.2 is negative


 

 

Conclusion : 

In this blog post, we have successfully created a Python program that checks whether a number is positive or negative. 

By accepting a single number from the user and applying a conditional statement, we can determine the nature of the number. 

Python's simplicity and readability make it an excellent choice for such tasks. You can further enhance the program by adding error handling for invalid input or expanding it to handle multiple numbers.

Post a Comment

0 Comments