Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Prime Numbers

Former Member
0 Likes
1,733

Hey everyone, I am given this assignment about ABAP programming. The program accepts 2 user input, 1 being min_num and 1 max_num. The minimum number should be 2 and the maximum number is 200. The program will thus prints out an output as follows:

Minumum Number : 2

Maximum Number : 10

Prime Numbers:

2

3

5

7

Can anyone pls help?

Hey everyone, I am given this assignment about ABAP programming. The program accepts 2 user input, 1 being min_num and 1 max_num. The minimum number should be 2 and the maximum number is 200. The program will thus prints out an output as follows:

Minumum Number : 2

Maximum Number : 10

Prime Numbers:

2

3

5

7

Can anyone pls help?

5 REPLIES 5
Read only

Former Member
0 Likes
1,139

Qi,

Look at this thread, you will get some ideas.

/thread/71937 [original link is broken]

Regards,

Ravi

Note : Please mark the helpful answers.

Read only

Former Member
0 Likes
1,139
PARAMETERS low TYPE i.
PARAMETERS high TYPE i.

DATA num TYPE i.
DATA is_prime.

num = low.
DO.
  PERFORM prime.
  IF is_prime EQ 'X'.
    WRITE: / num.
  ENDIF.
  num = num + 1.
  IF num GT high.
    EXIT.
  ENDIF.
ENDDO.


FORM prime.
DATA i TYPE i VALUE 2.
DATA mod TYPE i.
is_prime = 'X'.
DATA max TYPE i.
DATA cnt TYPE i VALUE 0.
max = num / 2.
IF num EQ 4.
  is_prime = ' '.
  EXIT.
ENDIF.
DO.
  IF i GE max.
    is_prime = 'X'.
    EXIT.
  ENDIF.
  mod = num MOD i.
  IF mod EQ 0. "Not prime
    is_prime = ' '.
    EXIT.
  ENDIF.
  cnt = cnt + 1.
  IF cnt GT max.
    EXIT.
  ELSE.
    i = i + 1.
  ENDIF.
ENDDO.

Read only

Former Member
0 Likes
1,139

hi Lim,

 parameters : lo_limit type i ,hi_limit type i.
data :sqrnum type i ,
flag type i , num1 type i.
while lo_limit lt 5 and lo_limit ge 0.
 lo_limit = lo_limit + 1.
  if lo_limit ne 1 and lo_limit ne 4.
  write : / lo_limit.
  endif.
endwhile.
lo_limit = lo_limit + 1.

while lo_limit ne hi_limit.
 flag = 0.
   sqrnum = lo_limit div 2.
   while sqrnum ne 2.
   num1 = lo_limit mod sqrnum.
   if num1 eq 0.
     flag = 1.
   endif.
   sqrnum = sqrnum - 1.
   endwhile.
 if flag eq 1.
 lo_limit = lo_limit + 1.
 continue.
 else.
 write : / lo_limit .
 endif.

regards

satesh

Read only

Former Member
0 Likes
1,139

Hi,

This is a simple program to print the prime numbers .

You just give the initial and final values.It will print the numbers for you..

REPORT ZSHAIL_PRIME .

data: num type i .

data: i type i,

j type i,

c type i,

modu type i.

select-options: value for num.

i = 1.

while i le value-high.

c = 0.

j = 1.

while j le i.

modu = i mod j.

if modu = 0.

c = c + 1.

endif.

j = j + 1.

endwhile.

if c = 2.

write: / i.

endif.

i = i + 1.

endwhile.

Regards,

Sylendra.

Read only

0 Likes
1,139

sample code...

REPORT ztest1 .

PARAMETERS: p_from TYPE i,

p_to TYPE i.

DATA: v_from TYPE i,

v_to TYPE i,

v_remainder TYPE i,

v_is_prime TYPE c,

v_divisor TYPE i.

START-OF-SELECTION.

v_from = p_from.

v_to = p_to.

DO.

IF v_from = v_to.

EXIT.

ENDIF.

CLEAR v_is_prime.

DO v_from TIMES.

v_divisor = sy-index.

v_remainder = v_from MOD v_divisor.

IF v_divisor <> 1 AND

v_divisor <> v_from AND

v_remainder = 0.

*-- not a prime

v_is_prime = 'N'.

EXIT.

ENDIF.

ENDDO.

IF v_is_prime IS INITIAL.

*-- the number is prime

WRITE:/ v_from, 'is a prime number'.

ENDIF.

v_from = v_from + 1.

ENDDO.