GeekInterview.com
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

Showing Answers 1 - 3 of 3 Answers
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.

  
Login to rate this answer.
drcobol

Answered On : Nov 26th, 2012

View all answers by drcobol

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)

  
Login to rate this answer.
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.

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.