Application Development 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: 

print prime numbers using string

0 Kudos

Accept a number and print all the prime numbers up to the number

example:

P_NO 100

2,3,5,7,11,13.......97

3 REPLIES 3

Former Member
0 Kudos

Welcome to the SAP Community. Thank you for visiting us to get answers to your questions.

Since you're asking a question here for the first time, I'd like to offer some friendly advice on how to get the most out of your community membership and experience.

First, please see https://community.sap.com/resources/questions-and-answers, as this resource page provides tips for preparing questions that draw responses from our members. Second, feel free to take our Q&A tutorial at https://developers.sap.com/tutorials/community-qa.html, as that will help you when submitting questions to the community.

I also recommend that you include a profile picture. By personalizing your profile, you encourage readers to respond: https://developers.sap.com/tutorials/community-profile.html.

Now for some specific suggestions on how you might improve your question:

* Outline what steps you took to find answers (and why they weren't helpful) -- so members don't make suggestions that you've already tried.

* Share screenshots of what you've seen/done (if possible), as images always helps our members better understand your problem.

* Make sure you've applied the appropriate tags -- because if you don't apply the correct tags, the right experts won't see your question to answer it.

Should you wish, you can revise your question by selecting Actions, then Edit.

The more details you provide (in questions tagged correctly), the more likely it is that members will be able to respond. As it stands, I don't know if there is enough information here for members to understand your issue. So please consider revising your question because I'd really like to see you get a solution to your problem!

I hope you find this advice useful, and we're happy to have you as part of SAP Community!

Sandra_Rossi
Active Contributor
0 Kudos

What could be the algorithm to find out if one number is a PRIME number?

After that, the rest is easy.

Nitish2027
Participant
0 Kudos

Use the below Code:

  DATA: p_num TYPE string VALUE 100.
TYPES: BEGIN OF ty_int,
num TYPE int2,
END OF ty_int.
DATA: lt_int TYPE TABLE OF ty_int,
lt_int1 TYPE TABLE OF ty_int.

DATA: lv_flag TYPE int2.
DATA: lv_temp TYPE int2 VALUE 1.
DATA: text TYPE string VALUE 'Prime Numbers: '.

lt_int = VALUE #( FOR i = 2 THEN i + 1 UNTIL i >= p_num
( num = i )
).

LOOP AT lt_int INTO DATA(ls_int).

lv_flag = 0.

LOOP AT lt_int INTO DATA(ls_int1) WHERE num <= ls_int-num.

lv_temp = ls_int-num MOD ls_int1-num.

IF lv_temp = 0.
lv_flag = lv_flag + 1.
ENDIF.

ENDLOOP.

IF lv_flag = 1.
text = text && | { ls_int-num }| && |,| .
ENDIF.

ENDLOOP.

WRITE:/ text.

This will give you your desired output:

You can also try to do this with new ABAP syntaxes using REDUCE and FOR operators.

Thanks and Regards,

Nitish Singh.