Algorithm
-
Input: Take a number as input.
-
Check if the number is zero:
- If the number is equal to zero, display "The number is zero" and end the program.
- If not, proceed to the next step.
-
Check if the number is positive:
- If the number is greater than zero, display "The number is positive" and end the program.
- If not, proceed to the next step.
-
Check if the number is negative:
- If the number is less than zero, display "The number is negative" and end the program.
- If not, the number is not zero, positive, or negative (this should not happen in a typical number system).
Code Examples
#1 Code Example- Check Number Type with if...else if...else
Code -
Javascript Programming
// program that checks if the number is positive, negative or zero
// input from the user
const number = parseInt(prompt("Enter a number: "));
// check if number is greater than 0
if (number > 0) {
console.log("The number is positive");
}
// check if number is 0
else if (number == 0) {
console.log("The number is zero");
}
// if number is less than 0
else {
console.log("The number is negative");
}
Copy The Code &
Try With Live Editor
Output
Enter a number: 0
The number is zero.
The number is zero.
#2 Code Example- Check Number Type with nested if...else
Code -
Javascript Programming
// check if the number is positive, negative or zero
const number = prompt("Enter a number: ");
if (number >= 0) {
if (number == 0) {
console.log("The number is zero");
} else {
console.log("The number is positive");
}
} else {
console.log("The number is negative");
}
Copy The Code &
Try With Live Editor
Output
Enter a number: 0
You entered number zero
You entered number zero
Demonstration
Javascript Programing Example to Check if a number is Positive, Negative, or Zero-DevsEnv