Preface – This post is part of the ABAP Programs series.
To check even or odd in ABAP Program, you need to take input from user using PARAMETERS and divide it by 2. The number which when divided by 2 gives remainder zero is called Even number while the number which gives remainder one is called odd number.
To get remainder in ABAP we use mod keyword. In the program given below we will find whether the number entered is Odd or Even .
PARAMETERS: p_num TYPE int2. IF p_num IS INITIAL. MESSAGE 'Input cannot be empty' TYPE 'I'. ELSEIF p_num MOD 2 EQ 0. WRITE 'EVEN'. ELSEIF p_num MOD 2 <> 0. WRITE 'ODD'. ENDIF.
Line 01: Here we have defined a parameter p_num to take input. The type of input will always be a number.
Line 02: If the input provided by user is initial, then in Line 03 we will pop up an information message saying “Input cannot be empty”.
Line 04: If the number provided by user is not initial and is divisible by 2, then we will give output as “Even” in Line 05.
Line 06: If the number provided by user is not initial and is not divisible by 2, then we will give output as “Odd” in Line 07.
That’s it, we have successfully written code to check if a given number is even or odd in ABAP Program.