cancel
Showing results for 
Search instead for 
Did you mean: 

creating diamond shaped output in sap abap editor.

0 Kudos
700

hi i am fresher and learning abap. while going through do and enddo statement. i wanted to create a diamond shaped output.

*

* *

* * *

can anyone help me to create the above output by playing in write statement or do, enddo loop statements.

Accepted Solutions (1)

Accepted Solutions (1)

MateuszAdamus
Active Contributor

Hello karoncdas_31

Maybe something like this?

PARAMETERS: p_size TYPE i.

DATA:
  lv_size TYPE i,
  lv_size_even TYPE flag,
  lv_step_size TYPE i,
  lv_number_of_stars TYPE i,
  lv_number_of_spaces TYPE i.

lv_size = p_size.
lv_size_even = xsdbool( lv_size MOD 2 = 0 ).
lv_step_size = 1.
lv_number_of_stars = 1.
IF lv_size_even = abap_true.
  lv_number_of_stars = 2.
ENDIF.

DO lv_size TIMES.
  lv_number_of_spaces = 0.
  IF lv_number_of_stars <> lv_size.
   lv_number_of_spaces = ( lv_size - lv_number_of_stars ) / 2.
  ELSE.
    IF lv_size_even = abap_true.
      IF lv_step_size = 0.
        lv_step_size = -1.
      ELSE.
        lv_step_size = 0. " double center line
      ENDIF.
    ELSE.
      lv_step_size = -1.
    ENDIF.
  ENDIF.

  WRITE / ' '.
  DO lv_number_of_spaces TIMES.
    WRITE ' '.
  ENDDO.

  DO lv_number_of_stars TIMES.
    WRITE '*'.
  ENDDO.

  DO lv_number_of_spaces TIMES.
    WRITE ' '.
  ENDDO.

  lv_number_of_stars = lv_number_of_stars + ( 2 * lv_step_size ). " times two, because from both sides
ENDDO.
Kind regards,
Mateusz
0 Kudos

Thank you mateusz for your response. I am grateful. As a fresher i am having a problem in understanding all the concepts used here. Is there any simpler way to explain this code please let me know. Thank you again for your time and consideration.

MateuszAdamus
Active Contributor

Hello Karon

What is it that you do not understand?

As far as the ABAP keywords go, you could paste the code into your ABAP editor, put cursor on any of the keywords and press the F1 button. This will show you the built in ABAP Help. I would strongly encourage you to read it first. It really gives a lot of valuable information, especially if you're new to the language.

Kind regards,
Mateusz
0 Kudos

can you explain me the concept/flow in which the code is written.

As i need * as a initial line output with certain amout of spaces in the beginning and ending so that it will look as a pyramid while the next lines were written with respective spaces as per our requirement.

0 Kudos

And when i try to copy paste the code. syntax error occured. attached image for reference.

MateuszAdamus
Active Contributor
0 Kudos

Hello

I do not see any image. Please try again.

The flow is as follows:

1. Get the size and check if it's odd or even.

2. Do logic number of times equal to the size of the diamond.

2.a. Get number of spaces before and after the stars.

2.b. Show spaces, starts and spaces.

Kind regards,
Mateusz
Sandra_Rossi
Active Contributor
Karon Christydoss "REPORT." is missing at the top maybe? If so, please learn the basics of ABAP, the forum goal is not to give lessons.
REPORT.
WRITE 'Hello world'.

Use the debugger for understanding the logic. It's the same as for any other programming language.

Answers (0)