Positive or Negative Number Check¶
Concept Explanation¶
What is it?¶
This program classifies a numerical input as either positive (greater than zero), negative (less than zero), or zero (equal to zero). It's a fundamental conditional check based on the number line.
Why is it important?¶
Identifying the sign of a number is a basic yet crucial operation in many programming scenarios. It dictates different behaviors, calculations, or validations depending on whether a value represents an increase, a decrease, or no change.
Where is it used?¶
- Financial Applications: Checking if a balance is positive (credit) or negative (debt).
- Game Development: Determining if a player has positive health, negative score, or zero resources.
- Data Validation: Ensuring that input values fall within an expected range (e.g., ages must be positive).
- Control Systems: Adjusting actions based on sensor readings being above, below, or at a set point.
Real-world example¶
When you check your bank account balance, if it's positive, you have money. If it's negative, you're overdrawn. If it's exactly zero, your account is empty. This program performs the same basic classification.
Algorithm¶
- Start.
- Get a number (
num) from the user. - If
numis greater than 0: a. Display thatnumis positive. - Else if
numis less than 0: a. Display thatnumis negative. - Else (
numis equal to 0): a. Display thatnumis zero. - End.
Edge Cases:
- Non-numeric input (handled by language-specific error mechanisms or explicit validation).
- Floating-point numbers: Comparisons should work correctly for double or float types.
Implementations¶
import java.util.Scanner;
public class PositiveOrNegative {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double number = scanner.nextDouble();
System.out.println("Number entered: " + number);
if (number > 0) {
System.out.println("The number is POSITIVE.");
} else if (number < 0) {
System.out.println("The number is NEGATIVE.");
} else {
System.out.println("The number is ZERO.");
}
scanner.close();
}
}
SET SERVEROUTPUT ON;
DECLARE
input_num NUMBER := &Enter_a_Number;
BEGIN
DBMS_OUTPUT.PUT_LINE('Input Number: ' || input_num);
IF input_num > 0 THEN
DBMS_OUTPUT.PUT_LINE('The number is POSITIVE.');
ELSIF input_num < 0 THEN
DBMS_OUTPUT.PUT_LINE('The number is NEGATIVE.');
ELSE
DBMS_OUTPUT.PUT_LINE('The number is ZERO.');
END IF;
END;
/
Explanation¶
- Java: Uses
if-else if-elsestructure with>and<comparison operators.Scannerhandlesdoubleinput, suitable for any real number. - Python: Employs
if-elif-elsestatements.float()converts string input to numbers. The comparison logic is straightforward. - C: Reads
doubleinput usingscanf("%lf", ...). The comparisons are performed withif-else if-else, andprintf()displays the result. - Oracle: Implemented in PL/SQL. Uses
NUMBERdata type for numerical comparison withinIF-ELSIF-ELSEblocks. Substitution variables for interactive input.
Complexity Analysis¶
- Time Complexity: O(1) - A fixed number of comparisons are performed.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Get Number]
B --> C{Number > 0?}
C -- Yes --> D[Display POSITIVE]
C -- No --> E{Number < 0?}
E -- Yes --> F[Display NEGATIVE]
E -- No --> G[Display ZERO]
D --> H[End]
F --> H
G --> H
Sample Dry Run¶
| Step | Number | Number > 0? | Number < 0? | Description |
|---|---|---|---|---|
| Input | 10 | - | - | User enters 10 |
| Decision | 10 | True | - | 10 is greater than 0 |
| Output | - | - | - | Display "The number is POSITIVE." |
| End | - | - | - | Program terminates |
| Input | -5 | - | - | User enters -5 |
| Decision | -5 | False | - | -5 is not greater than 0 |
| Decision | -5 | - | True | -5 is less than 0 |
| Output | - | - | - | Display "The number is NEGATIVE." |
| End | - | - | - | Program terminates |
| Input | 0 | - | - | User enters 0 |
| Decision | 0 | False | - | 0 is not greater than 0 |
| Decision | 0 | - | False | 0 is not less than 0 |
| Output | - | - | - | Display "The number is ZERO." |
| End | - | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to check if the number is positive, negative, or exactly equal to a specific value (e.g., 100).
- Extend the program to check if a number is even or odd.
Medium¶
- Implement a function that takes a list of numbers and returns counts of positive, negative, and zero values.
- Add input validation to ensure the user enters a valid number.
Hard¶
- Create a system that categorizes a set of financial transactions as income (positive), expense (negative), or neutral.
"The only way to do great work is to love what you do." - Steve Jobs