0% found this document useful (0 votes)
2K views3 pages

Find Prime Number Using Cobol

The document describes two methods for finding prime numbers using COBOL. Method 1 uses a loop to divide the input number by all integers from 1 to the number to check if it is divisible, indicating it is not prime. Method 2 also uses a loop to check for divisibility but only goes up to half the number. Both display whether the number is prime or not at the end. A third section shows a COBOL program to calculate Fibonacci numbers from 1 to 100.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views3 pages

Find Prime Number Using Cobol

The document describes two methods for finding prime numbers using COBOL. Method 1 uses a loop to divide the input number by all integers from 1 to the number to check if it is divisible, indicating it is not prime. Method 2 also uses a loop to check for divisibility but only goes up to half the number. Both display whether the number is prime or not at the end. A third section shows a COBOL program to calculate Fibonacci numbers from 1 to 100.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

*****FIND PRIME NUMBER USING COBOL******

Method-1

IDENTIFICATION  DIVISION  
PROGRAM-­‐ID.  PRIME.  
ENVIRONMENT  DIVISION.  
DATA  DIVISION.  
     WORKING-­‐STORAGE  SECTION.  
               77  N  PIC  9(3).  
               77  Q  PIC  9(3).  
               77  R  PIC  9(3).  
               77  I  PIC  9(3)  VALUE  1.  
 PROCEDURE  DIVISION.  
               PARA-­‐A.  
                               DISPLAY  (  1  ,  1  )  ERASE.  
                               DISPLAY  (  2  ,  1  )  "ENTER  AN  INTEGER:".  
                               ACCEPT  (  2  ,  20  )  N.  
                               IF  N  =  1  
                                               DISPLAY  (  3  ,  1  )  "NUMBER  IS  NOT  PRIME"  
                                               GO  TO  STOP-­‐PARA.  
               PARA-­‐B.  
                               ADD  1  TO  I.  
                               IF  I  =  N  
                                               DISPLAY  (  3  ,  1  )  "NUMBER  IS  PRIME"  
                                               GO  TO  STOP-­‐PARA.  
 
                               DIVIDE  N  INTO  I  GIVING  Q  REMAINDER  R.  
 
                               IF  R  =  0  
                                               DISPLAY  (  3  ,  1  )  "NUMBER  IS  NOT  PRIME"  
                                               GO  TO  STOP-­‐PARA.  
                               GO  TO  PARA-­‐B.  
               STOP-­‐PARA.  
                               STOP  RUN.  
 
 
 
 
 
 
 
 
Method-2
 
IDENTIFICATION  DIVISION.  
PROGRAM-­‐ID.  PRIMENO.  
ENVIRONMENT  DIVISION.  
     CONFIGURATION  SECTION.  
           SOURCE-­‐COMPUTER.  IBM  PC.  
         OBJECT-­‐COMPUTER.  IBM  PC.  
DATA  DIVISION.  
   WORKING-­‐STORAGE  SECTION.  
             01    N      PIC    9(4).  
             01    C      PIC    9(4).  
             01    D      PIC    9(4).  
             01    I      PIC    9(4).  
PROCEDURE  DIVISION.  
           MAIN-­‐PARA.  
                       DISPLAY  'ENTER  THE  NUMBER  TO  CHECK  WHETHER  IT  IS  
PRIME'.  
                       ACCEPT  N.  
                       IF  N  =  1  DISPLAY  'THIS  IS  NOT  A  PRIME  NUMBER'  STOP  RUN.  
                       DIVIDE  N  BY  2  GIVING  C  ROUNDED.  
                       PERFORM  CHECK-­‐PARA  VARYING  I  FROM  1  BY  1  UNTIL  I  >  C.  
                       DISPLAY  'THE  NUMBER  IS  PRIME'.  
                       STOP  RUN.  
             CHECK-­‐PARA.  
DIVIDE  N  BY  I  GIVING  C  REMAINDER  D.  
                       IF  D  =  0  
                       DISPLAY  'THE  NUMBER  IS  NOT  PRIME'  STOP  RUN.  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
IDENTIFICATION  DIVISION.  
PROGRAM-­‐ID.  FIBONACCI.  
DATA  DIVISION.  
WORKING-­‐STORAGE  SECTION.  
               77  N    PIC  9(18).  
               77  N1  PIC  Z(18).  
               77  M    PIC  9(18)  VALUE  1.  
               77  O    PIC  9(18).  
               77  I    PIC  9(4)  VALUE  1.  
               77  Q    PIC  X.  
PROCEDURE  DIVISION.  
               PARA-­‐A.  
                               DISPLAY  (  1  ,  1  )  ERASE.  
                               DISPLAY  (  2  ,  1  )  "FIBONACCI  NUMBERS  FROM  1  TO  100:".  
                               MOVE  0  TO  N.  
                               DISPLAY  "  ".  
                               DISPLAY  0.  
                               DISPLAY  1.  
                               MOVE  0  TO  O.  
               PARA-­‐B.  
                               COMPUTE  N  =  O  +  M.  
                               MOVE  N  TO  N1.  
                               MOVE  M  TO  O.  
                               MOVE  N  TO  M.  
                               DISPLAY  N1.  
                               ADD  1  TO  I.  
                               IF  I  =  21  
                                               DISPLAY  "PRESS  TAB  KEY  TO  VIEW  NEXT  PAGE."  
                                               ACCEPT  Q.  
                               IF  I  =  41  
                                               DISPLAY  "PRESS  TAB  KEY  TO  VIEW  NEXT  PAGE."  
                                               ACCEPT  Q.  
                               IF  I  =  61  
                                               DISPLAY  "PRESS  TAB  KEY  TO  VIEW  NEXT  PAGE."  
                                               ACCEPT  Q.  
                               IF  I  =  81  
                                               DISPLAY  "PRESS  TAB  KEY  TO  VIEW  NEXT  PAGE."  
                                               ACCEPT  Q.  
                               IF  I  =  99  
                                               GO  TO  STOP-­‐PARA  
                               ELSE        
GO  TO  PARA-­‐B.  
                           STOP-­‐PARA.    
                           DISPLAY  "  ".              
                           STOP  RUN.  

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy