Register Login

Program For Printing Prime Number

Updated May 18, 2018

*&---------------------------------------------------------------------*
*& Report ZPRIME *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT zprime .
DATA : t_no TYPE i,
res TYPE p,
pr_no TYPE p,
tag TYPE i.
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS : pr_num TYPE i.
SELECTION-SCREEN END OF BLOCK b1.
pr_no = 1.
WHILE pr_no LE pr_num.
t_no = 2.
tag = 0.
WHILE t_no res = pr_no MOD t_no.
IF res = 0.
WRITE : / pr_no.
WRITE : 'is not PRIME Number ' COLOR 3 INTENSIFIED OFF.
tag = 1.
EXIT.
ENDIF.
t_no = t_no + 1.
ENDWHILE.
IF tag = 0.
WRITE : / pr_no.
WRITE : 'is PRIME Number ' COLOR 6 INTENSIFIED OFF.
ENDIF.
pr_no = pr_no + 1.
ENDWHILE.


Comments

  • 07 Jun 2017 1:51 pm Guest Helpful Answer

    This program will print prme numbers from 1  to p_num ( value given by iser inthe parameter).

     

    PARAMETERS: p_num TYPE i.
    DATA: n      TYPE i,
          lv_num TYPE i,
          lv_cnt TYPE i VALUE 2.

    lv_num = p_num.
    DO p_num TIMES.
      n = lv_num.
      WHILE lv_cnt <= lv_num / 2.
        IF ( n MOD lv_cnt ) = 0.
          DATA(lv_non_prime) = abap_true.
          EXIT.
        ENDIF.
        lv_cnt = lv_cnt + 1.
      ENDWHILE.

      IF lv_non_prime = abap_false.
        WRITE n.
      ENDIF.
      lv_cnt = 2.
      CLEAR lv_non_prime.
      lv_num = lv_num - 1.
    ENDDO.


×