num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
Code Explanation
Input
num = float(input("Enter a number: "))
input(): Prompts the user to enter a number as a string.
float(): Converts the input string into a floating-point number (to handle both integers and decimal numbers).
For example, entering 5 converts to 5.0, and -3.7 remains -3.7.
Conditional Statements
The program checks the value of the input number using if-elif-else.
If the number is greater than 0:
if num > 0:
print("Positive number")
If the number is greater than 0, it is classified as a positive number.
Example: For num = 5, it prints:
Positive number
If the number is less than 0:
elif num < 0:
print("Negative number")
If the number is less than 0, it is classified as a negative number.
Example: For num = -3.7, it prints:
Negative number
If the number is neither greater than nor less than 0:
else:
print("Zero")
If the number is exactly 0, it falls into the else case and prints:
Zero
#source code --> clcoding.com
0 Comments:
Post a Comment