Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Example Programs
  5. Find Negative Positive and Zero

Find Negative Positive and Zero

Example 20

Write a JavaScript that reads 10 numbers from user and store it in array. Display the count of negative numbers, positive numbers and zero from the list.

Code

const prompt = require("prompt-sync")();
const input = parseInt(prompt("Enter the number of elements : "));
let a = new Array();
let pos_count = 0;
let neg_count = 0;
let zero_count = 0;
for (let i = 0; i < input; i++) {
  let t = prompt("enter the number for \t" + i + " \t location \t");
  a[i] = parseInt(t); // convert string to integer
}
console.log("Elements are:");
for (let i = 0; i < input; i++) {
  console.log("a[" + i + "]:" + a[i]);
}

for (let i = 0; i < input; i++) {
  if (a[i] > 0) {
    pos_count++;
  } else if (a[i] < 0) {
    neg_count++;
  } else {
    zero_count++;
  }
}
console.log(`positives : ${pos_count}`);
console.log(`negatives : ${neg_count} `);
console.log(`zeros : ${zero_count}`);

Output

Loading

Views: 104

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments