//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com

ASP FAQ Tutorials :: General Concepts :: How do I determine if a number is odd or even?


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

Sometimes you need to know this because it's important; other times, it's simply for flipping the colors of adjacent rows in an HTML table. In any case, here are some samples of using the MODULUS operator on signed integers in T-SQL, VBScript and JScript: 
 
Transact-SQL 
This mimics retrieving a resultset from a table, along with a description of whether each id is odd or even. Note that ABS() is used so that the sign is ignored. 
 
SELECT 
    id, 
    CASE WHEN ABS(id) % 2 = 1 
        THEN 'odd' 
        ELSE 'even' 
    END 
FROM table 
 
VBScript 
This uses a hardcoded value, so you can switch it around and test it before incorporating it into more significant code. 
 
<% 
    id = 5      
    stat = "even" 
    SELECT CASE abs(id) mod 2 
        CASE 1: stat = "odd" 
    END SELECT 
    Response.write id & " is " & stat 
%>
 
JScript 
This sample is just like the VBScript code above. The additional use of the Math object was required to convert the integer to positive. 
 
<script language='JScript' runat=server> 
    var id = 5; 
    var stat = "even"; 
    switch(Math.abs(id) % 2) 
    { 
        case 1 : stat = "odd"; 
    } 
    Response.Write(id.toString() + " is " + stat); 
</script>

 

 


Created: 10/28/2001 | Last Updated: 6/16/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (40)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...