Thursday, 28 November 2024

Day 5 : Python Program to Check Whether a Number is Positive or Negative

 


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

Popular Posts

Categories

100 Python Programs for Beginner (78) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (950) Python Coding Challenge (392) Python Quiz (46) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses