Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether a variable has an even numeric value?

Tags:

bash

How should I change this to check whether val has an even or odd numeric value?

val=2 if $((RANDOM % $val)); ... 
like image 898
lizarisk Avatar asked Mar 27 '13 13:03

lizarisk


People also ask

How do you check if a number is even?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd.

How do you determine if a number is odd or even?

To tell whether a number is even or odd, look at the number in the ones place. That single number will tell you whether the entire number is odd or even. An even number ends in 0, 2, 4, 6, or 8. An odd number ends in 1, 3, 5, 7, or 9.

How do you check if a variable contains a number in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.


2 Answers

$ a=4  $ [ $((a%2)) -eq 0 ] && echo "even" even  $ a=3  $ [ $((a%2)) -eq 0 ] && echo "even" 
like image 135
Fredrik Pihl Avatar answered Oct 02 '22 13:10

Fredrik Pihl


foo=6  if [ $((foo%2)) -eq 0 ]; then     echo "even"; else     echo "odd"; fi 
like image 30
Paul Avatar answered Oct 02 '22 14:10

Paul