animis
0
Q:

how to check if a number is even or odd in javascript

if ( num % 2 == 0) {
	alert('Even Number');
}else{
	alert('Odd Number');
}
2
function isOdd(num) { return num % 2;}
console.log("1 is " + isOdd(1));
console.log("2 is " + isOdd(2));
console.log("3 is " + isOdd(3));
console.log("4 is " + isOdd(4));
3
function evenOrOdd(num) { //for string length take string
  if (num % 2 == 0) {//and replace numm with string.length here
    return "even";
  } else {
    return "odd";
  }
}
0
// C++ program to check for even or odd 
// using Bitwise XOR operator 
  
#include <iostream> 
using namespace std; 
  
// Returns true if n is even, else odd 
bool isEven(int n) 
{ 
  
    // n^1 is n+1, then even, else odd 
    if (n ^ 1 == n + 1) 
        return true; 
    else
        return false; 
} 
  
// Driver code 
int main() 
{ 
    int n = 100; 
    isEven(n)  
? cout << "Even" 
: cout << "Odd"; 
  
    return 0; 
} 
0
<!DOCTYPE html>
<html>
<body>
<script>
   var num = 5;
   document.write("Number = "+num+"<br>");
   if(num % 2 == 0) {
      document.write('Number is even!');
   } else {
      document.write('Number is odd!');
   }
</script>
</body>
</html>
0

New to Communities?

Join the community