Coding Logic to Print Stars

How to code abap logic to print stars like this:

   *

* *

* * * *

* *
   *
Note: no. of maximum stars will be entered by user.

Check this code:

parameters:

  p type i default 5.

data:

  x type i,
  y type i.

  x = ( p + 1 ) / 2 .

  do x times.
    y = 2 * sy-index - 1.

    do y times.
      write '*'.

    enddo.

    write /.

  enddo.

  x = x - 1.

  do  x  times.
    y = y - 2.

    do y times.
      write '*'.

    enddo.

    write /.

  enddo.
 

Another example:

Note : p_star should be odd

Report z_test.

PARAMETERS: P_STAR TYPE I.
DATA lv_1 TYPE I.
DATA V_FLAG.

START-OF-SELECTION.
lv_1 = ( P_STAR / 2 ).
IF lv_1 = 0.
lv_1 = 2.
ELSE.
lv_1 = 1.
ENDIF.
DO P_STAR TIMES.
DO lv_1 TIMES.
WRITE: '*'.
ENDDO.
SKIP.
IF lv_1 = P_STAR.
V_FLAG = 'X'.
ENDIF.
IF V_FLAG IS INITIAL.
lv_1 = lv_1 + 2.
ENDIF.
IF V_FLAG IS NOT INITIAL.
lv_1 = lv_1 - 2.
ENDIF.
ENDDO.

p_star : 5

Output:

test 
• 

* * 

* * * * 

* * 

I tested it, so it should work.

ABAP

Read Also
An Example of a ListBox Program

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.