Series: Subject: Topic:
Question: 4 of 231

# prime number in COBOL

culd any body tell the concept for the prime number programe?
Asked by: vijayjessy | Member Since Jun-2012 | Asked on: Aug 9th, 2012

View all questions by vijayjessy

Sravani

Answered On : 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.

drcobol

Answered On : 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)

Gunesh Asatkar

Answered On : Feb 13th, 2013

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

```CodeDATA DIVISION.
WORKING-STORAGE SECTION.
77 I  PIC 999 VALUE 001.
77 N PIC 999 VALUE 030.
77 M PIC 999 VALUE 1.
77 A PIC 999.
77 Q PIC 999.
77 R PIC 999.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "PRIME NUMBER GENERATION".
DISPLAY "PRIME NUMBER START FROM" I.
DISPLAY "PRIME NUMBER END " N.
PERFORM LOOP UNTIL I>N.
GO TO STOP-PARA.
LOOP.
PERFORM VARYING K FROM 1 BY 1 UNTIL K > I
DIVIDE I BY K GIVING Q REMAINDER R
IF R = 0
COMPUTE A =A+1
END-IF
END-PERFORM.
IF A = 2
DISPLAY I
END-IF
MOVE 0 TO A
COMPUTE I = I+1.
STOP-PARA.
STOP RUN.```