Coding Problem Solutions 1-3
Coding Problem Solutions 1-3
Solution Code:
Q. Write a program to check the given number (const num = 2541;) is odd or even
Solution Code:
if (num % 2 == 0) {
console.log("The given number is even.");
} else {
console.log("The given number is odd.");
}
Q. Write a program to find whether the given number is present in the given array or not using
the switch case.
Input:
const arr = [2,4,5,9,3,1,8];
const num = 5;
Solution code:
CP23.js
const arr = [2,4,5,9,3,1,8];
const num = 5;
switch (num) {
case (arr[0]):
console.log('num is found');
break;
case (arr[1]):
console.log('num is found');
break;
case (arr[2]):
console.log('num is found');
break;
case (arr[3]):
console.log('num is found');
break;
case (arr[4]):
console.log('num is found');
break;
case (arr[5]):
console.log('num is found');
break;
case (arr[6]):
console.log('num is found');
break;
default:
console.log('num is not found');
}