Prime number in COBOL

Culd any body tell the concept for the prime number programe?

Questions by vijayjessy

Showing Answers 1 - 9 of 9 Answers

Sravani

  • Aug 29th, 2012
 

One of the logic is:

Check the number for divisibility from 2 to till the number and if the reminder is 0 increment the counter and finally check if the counter>1 the number is not Prime.

  Was this answer useful?  Yes

drcobol

  • Nov 26th, 2012
 

1. You dont need to loop further than the integer of the square root of the number in question
2. You can skip multiples of numbers already checked (if a number is not divisible by 2 then its not by 4 either)

  Was this answer useful?  Yes

Gunesh Asatkar

  • Feb 13th, 2013
 

Set the start and end limit from which prime will calculate.
then start first for loop and second for loop.

Code
  1. DATA DIVISION.

  2. WORKING-STORAGE SECTION.

  3. 77 I  PIC 999 VALUE 001.

  4. 77 N PIC 999 VALUE 030.

  5. 77 M PIC 999 VALUE 1.

  6. 77 A PIC 999.

  7. 77 Q PIC 999.

  8. 77 R PIC 999.

  9. PROCEDURE DIVISION.

  10. MAIN-PARA.

  11.        DISPLAY "PRIME NUMBER GENERATION".

  12.        DISPLAY "PRIME NUMBER START FROM" I.

  13.        DISPLAY "PRIME NUMBER END " N.

  14.        PERFORM LOOP UNTIL I>N.

  15.        GO TO STOP-PARA.

  16. LOOP.

  17.         PERFORM VARYING K FROM 1 BY 1 UNTIL K > I

  18.         DIVIDE I BY K GIVING Q REMAINDER R

  19.         IF R = 0

  20.               COMPUTE A =A+1

  21.         END-IF

  22.         END-PERFORM.

  23.         IF A = 2

  24.         DISPLAY I

  25.         END-IF

  26.         MOVE 0 TO A

  27.         COMPUTE I = I+1.

  28. STOP-PARA.

  29.         STOP RUN.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions