Wednesday, December 17, 2025

Determine if a number is even or odd

Have you ever had the need to determine if a number is even or odd? I have had to in various scenarios. This has been made a lot easier with the addition of a couple of scalar functions that have been added as part of the latest Technology Refreshes.

In the past I would need to check the remainder of dividing a number by two.

If I was to do this in RPG my program could look like:

01  **free
02  dcl-s Number packed(1 : 0) ;
03  dcl-s Remainder packed(1 : 0) ;

04  Number = 6 ;
05  Remainder = %rem(Number : 2) ;
06  dsply ('1. Remainder = ' + %char(Remainder)) ;

07  Number = 7 ;
08  Remainder = %rem(Number : 2) ;
09  dsply ('2. Remainder = ' + %char(Remainder)) ;

10  *inlr = *on ;

Lines 5 and 8: I am using the Remainder Built in Function, %REM, to return the value of the remainder into the variable Remainder. If the remainder is zero then the number used is even, if it is one then the number is odd.

When called the program the following is displayed:

DSPLY  1. Remainder = 0
DSPLY  2. Remainder = 1

The following does the same in SQL:

01  SELECT NUMBER,
02         CASE
03           WHEN MOD(NUMBER,2) = 0 THEN 'Even'
04           ELSE 'Odd'
05         END  AS "Even/Odd"
06   FROM NUMBERS

Line 2: I need to use a Case scalar function to check the number from the Table.

Line 3: The MOD will return the remainder for the calculation in the parameters pass to it. In this case the value from the Number column, divided by two. This line checks if the value returned from the MOD is zero then the number is even.

Line 4: If the value returned from MOD is not zero, the number is odd.

The results are:

NUMBER   Even/Odd
------   --------
     0   Even
     1   Odd
     2   Even
     3   Odd
     4   Even
     5   Odd

The new Technology Refreshes introduced two new scalar functions to Db2 for i:

  • EVEN
  • ODD

Both of the scalar functions return a Boolean value. The syntax of the scalar functions are:

  VALUES (SYSTOOLS.EVEN(2),SYSTOOLS.ODD(2))

The scalar functions are both in the library SYSTOOLS. I am checking if the number two is even in the first scalar function, and then if it is odd in the second. The results are:

00001   00002
-----   -----
true    false

If I modify the SQL statement I used with the Table NUMBERS to use these scalar functions it would look like:

01  SELECT NUMBER,
02         SYSTOOLS.EVEN(NUMBER) AS "Even?",
03         SYSTOOLS.ODD(NUMBER) AS "Odd?"
04    FROM NUMBERS

The results are:

NUMBER   Even?   Odd?
------   -----   ----
     0   true    false
     1   false   true
     2   true    false
     3   false   true
     4   true    false
     5   false   true

I can also use these in a RPG program:

01  **free
02  dcl-s Number packed(1 : 0) ;
03  dcl-s Even ind ;

04  Number = 6 ;
05  exec sql SET :Even = SYSTOOLS.EVEN(:Number) ;
06  dsply ('1. Even = ' + Even) ;

07  Number = 7 ;
08  exec sql SET :Even = SYSTOOLS.EVEN(:Number) ;
09  dsply ('2. Even = ' + Even) ;

10  *inlr = *on ;

Line 3: I need an indicator variable defined to store the Boolean value returned from the Scalar function.

Lines 5 and 8: I am using the SQL Set to take the result from the Even scalar function into the indicator variable, Even.

The results of the above program are:

DSPLY  1. Even = 1
DSPLY  2. Even = 0

What happens if I use the EVEN and ODD with numbers with decimals? According to my mathematics book:

Decimals cannot be even or odd because these terms only apply to integers (whole numbers).

What happens when I use numbers with decimals with these scalar functions?

01  VALUES (2.1,SYSTOOLS.EVEN(2.1),SYSTOOLS.ODD(2.1)),
02         (3.2,SYSTOOLS.EVEN(3.2),SYSTOOLS.ODD(3.2))  

The results appear to show that only the whole number part (the part to the left of the decimal point) is used.

00001   00002   00003
------  -----   -----
   2.1  true    false
   3.2  false   true

If null is used with these Scalar functions...

01  VALUES (SYSTOOLS.EVEN(NULL),SYSTOOLS.ODD(NULL))

The result is also null:

00001   00002
------  ------
<NULL>  <NULL>

Next time I need to determine if a value is even or odd these Scalar functions are going to make it a lot easier to code, and for others to understand.

 

You can learn more about this from the IBM website:

 

This article was written for IBM i 7.6 TR1 and 7.5 TR7.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.