Gnocl Cookbook‎ > ‎

Odd or Even?

Have you ever wanted a simple proc to let you know wether an integer is odd or even? If so, then the following code snippet should do the trick!

#---------------
# odd or even number?
#---------------
# returns 0 = even ; 1 = odd ;
#
proc odd_or_even {n} {

    set n [string trimleft $n -]
    set res 0
   
    for {set i 0} {$i <= $n} {incr i} {
        if {$n == $i} {return $res}
        if {$res == 0} {set res 1} else {set res 0}
    }

}

Comments