44.9k views
0 votes
Write a JavaScript program that will put an integer number and

tell whether the number is positive, negative or zero.

1 Answer

1 vote

Final answer:

The JavaScript program uses an if-else statement to check if an integer number is positive, negative, or zero by comparing it to zero and then logs an appropriate message to the console. It demonstrates how the sign of a number is determined relative to zero.

Step-by-step explanation:

JavaScript Program to Check if a Number is Positive, Negative, or Zero

To write a JavaScript program to check if an integer is positive, negative, or zero, we first need to understand the sign of a number. Numbers can either have a +ve (positive) or −ve (negative) sign, or no sign which implies a positive value. The sign is determined by whether the number is greater than, less than, or equal to zero.

Here is a simple program to check the sign of a number:

function checkNumberSign(number) {
if (number > 0) {
console.log(number + " is positive");
} else if (number < 0) {
console.log(number + " is negative");
} else {
console.log(number + " is zero");
}
}

// Example use:
checkNumberSign(3); // Output: 3 is positive
checkNumberSign(-1); // Output: -1 is negative
checkNumberSign(0); // Output: 0 is zero

This program uses an if-else statement to compare the input number with zero and log a message to the console accordingly. To run it, simply call the checkNumberSign function with an integer as an argument.

User Vadim Eksler
by
7.7k points