2009 Jan 12 2:30 PM
I have to test if a value is even. The value can be from 0 - 560. What is the simpliest (and most efficient) way of writing this routine
thanks
2009 Jan 12 2:34 PM
hi,
The best and easiest method..
Do 560 times.
lval = sy-index mod 2.
if lval EQ '0'.
EVEN
ELSE.
*ODD
ENDIF.
ENDIF.
2009 Jan 12 2:46 PM
hi,
DATA : num TYPE i,
rem TYPE i.
DO 560 TIMES.
rem = sy-index MOD 2.
IF rem = 0.
WRITE : / sy-index, '-even'.
ELSE.
WRITE : / sy-index, '-odd'.
ENDIF.
ENDDO.Thanks & Regards
2009 Jan 12 2:48 PM
The mod function is what you need.
DATA: mod_value TYPE i.
mod_value = 21 MOD 2 .
IF mod_value EQ '0'.
WRITE / 'even'.
ELSE.
WRITE / 'odd'.
ENDIF.
2009 Jan 13 7:29 AM
Hi Dominic ,
the efficient way is :
parameter : number .
DATA: mod TYPE i.
mod = number MOD 2 .
IF mod EQ '0'.
WRITE / 'Number is even'.
ELSE.
WRITE / ''Number is odd'.
ENDIF.
It will work most efficiently .
Regards ,
Nilesh Jain .
2009 Jan 13 8:04 AM
Hi,
the most efficient way to do this is
data : n type i.
do 280 times.
n = 2 * sy-index.
write 😕 'even', n.
enddo.
this is the shortest code and will do fast.
hope this will help you.
regards,
anand
2013 May 31 8:38 AM
REPORT Z_N_EVEN_NO.
DATA: V_NO TYPE I,
RESULT TYPE I.
PARAMETERS: P_NO TYPE I.
IF RESULT = P_NO MOD 2.
RESULT = 0.
WRITE:/ ' THE NO IS EVEN NO'.
ELSE.
WRITE:/ ' THE NO IS AN ODD NO'.
ENDIF.