Select Page

# How to make ABAP Calculator Program

by | May 30, 2018 | ABAP Programs

Preface – This post is part of the ABAP Programs series.

## Introduction

ABAP calculator program is just like any simple calculator which performs operation like addition, subtraction, multiplication and division.

A Calculator in ABAP is just like any simple calculator which performs operation like addition, subtraction, multiplication and division. To perform these operation we will take two inputs from user using PARAMETERS keyword and the operation in form of Push Button/RADIO Button.

Here in given program we have used Push Button to get the operation from user.

PARAMETERS : p_inp1 TYPE int2,
p_inp2 TYPE int2.
DATA: lv_out  TYPE int2,
lv_sign TYPE c,
flag    TYPE int1 VALUE 0.
SELECTION-SCREEN :BEGIN OF SCREEN 500 TITLE TEXT-001,  "Where we create a screen with number 500
PUSHBUTTON 25(10) sub USER-COMMAND sub,
PUSHBUTTON 40(10) mul USER-COMMAND multiply,
PUSHBUTTON 55(10) div USER-COMMAND divide,
END OF SCREEN 500.
INITIALIZATION. " Where we Initialize the value of our buttons we created above
sub = 'Subtract'.
mul = 'Multiply'.
div = 'Division'.
AT SELECTION-SCREEN. "Where we do calculation
CASE sy-ucomm.
flag = 1.
lv_out = p_inp1 + p_inp2.
WHEN 'SUB'.
flag = 1.
lv_out = p_inp1 - p_inp2.
WHEN 'DIVIDE'.
IF ( p_inp2 <> 0 ).
flag = 1.
lv_out = p_inp1 / p_inp2.
ELSE.
flag = 2.
ENDIF.
WHEN 'MULTIPLY'.
flag = 1.
lv_out = p_inp1 * p_inp2.
ENDCASE.
START-OF-SELECTION. " Where we show output
IF p_inp1 IS NOT INITIAL OR p_inp2 IS NOT INITIAL.
CALL SELECTION-SCREEN '500' STARTING AT 10 10. "Where we call the Screen 500 we created earlier.
IF flag = 1.
WRITE: lv_out.
ELSEIF flag = 2.
WRITE: 'Cannot Divide a number by 0'.
ELSEIF flag = 0.
MESSAGE 'Press any Button to perform any operation!' TYPE 'I'.
ENDIF.
ELSE.
MESSAGE 'Please give both Input to proceed!' TYPE 'I'.
ENDIF.

## Line by Line Explanation of Code

Line 1: Parameter declaration of type integer

Line 2: Parameter declaration of type integer

Line 3: Data declaration of type integer

Line 4: Data declaration of type character

Line 5: Data Declaration for Flag of type integer and value 0

Line 6: Selection Screen Statement and creation of Screen 500

Line 7: Creation of Push Button ‘add’ with User Command ‘create’ [User Command is used to call the button]

Line 8: Creation of Push Button ‘sub’ with User Command ‘sub’ [User Command is used to call the button]

Line 9: Creation of Push Button ‘mul’ with User Command ‘multiply’ [User Command is used to call the button]

Line 10: Creation of Push Button ‘div’ with User Command ‘divide’ [User Command is used to call the button]

Line 11: End of Selection Screen made at Line 6

Line 12: INITIALIZATION Event [It is a predefined event of ABAP Reports]

Line 14: Naming the button sub as ‘Subtract’

Line 15: Naming the button mul as ‘Multiply’

Line 16: Naming the button div as ‘Division’

Line 17: AT SELECTION-SCREEN Event [It is a predefined event of ABAP Reports]

Line 18: Create of Case statement where the user input will be taken via System Variable sy-ucomm and will be the condition for further statement.

Line 19: When user clicked button ‘ADD’ that we created in Line 7, then proceed to next statement else jump to next When condition.

Line 22: When user clicked button ‘SUB’ that we created in Line 8, then proceed to next statement else jump to next When condition.

Line 25: When user clicked button ‘DIVIDE’ that we created in Line 9, then proceed to next statement else jump to next When condition.

Line 32: When user clicked button ‘MULTIPLY’ that we created in Line 10, then proceed to next statement else jump to next When condition.

In above condition, we have then written simple mathematical statements to perform respective operations.

That’s it, we have successfully written code for ABAP Calculator Program.

# 1 Comment

1. 540/690*100 = 78.2 Executive coding plzz