Problem 14. odd_even.js
/*Question
Count odd even numbers
Count number of odd and even number in given list.
Input
First line contains length of the list. Each line contains integer specifying each element in
list.
Output
2 integers in each line specifying count of odd and even numbers respectively.
Example
Input:
5
12
14
15
13
18
Output:
2
3
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
//..............................................................................................................................
let lengthOfSeq = parseInt(readLine()); // 5
let odd_count = 0;
let even_count = 0;
for (let i = 0; i < lengthOfSeq; i++) {
let integers = parseInt(readLine()); // this will read the numbers one by one
if (integers % 2 != 0) {
odd_count++;
}
else {
even_count++;
}
}
console.log(odd_count);
console.log(even_count);
Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node odd_even.js <input.txt
2
3
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment