write a cobol program to display prime numbers between 1 to 100?

Answers were Sorted based on User's Feedback



write a cobol program to display prime numbers between 1 to 100?..

Answer / chandrababu

Please use below code to display prime number between 1 and N


0001.00 IDENTIFICATION DIVISION.
0002.00 PROGRAM-ID. COBOL31.
0003.00 AUTHOR. DURGA400.
0003.01 DATA DIVISION.
0003.02 WORKING-STORAGE SECTION.
0003.03 77 VAR PIC 9(3) VALUE 2.
0003.04 77 NUM PIC 9(3).
0003.05 77 REM PIC 99.
0003.06 77 REM1 PIC 99.
0003.07 77 QNT PIC 99.
0003.08 77 CNT PIC 99.
0004.00 PROCEDURE DIVISION.
0005.01 ACCEPT NUM.
0005.13 PERFORM 100-PARA TEST AFTER UNTIL VAR > NUM.
0005.14 100-PARA.
0005.15 IF VAR = 2
0005.16 DISPLAY "PRIME NUMBER " VAR
0005.17 ELSE
0005.19 PERFORM 200-PARA VARYING CNT FROM 2 BY 1 UNTIL CNT >= VAR
0005.20 DISPLAY "PRIME NUMBER " VAR
0005.21 END-IF.
0005.22 COMPUTE VAR = VAR + 1.
0005.25 999-END.
0005.26 STOP RUN.
0005.27 HALT.
0005.28 200-PARA.
0005.29 DIVIDE VAR BY CNT GIVING QNT REMAINDER REM
0005.30 IF REM = 0
0005.31 COMPUTE VAR = VAR + 1
0005.32 GO TO 100-PARA
0005.33 END-IF.
****************** End of data ************


Regards,
Chandrababu

Is This Answer Correct ?    9 Yes 3 No

write a cobol program to display prime numbers between 1 to 100?..

Answer / surya

IDENTIFICATION DIVISION.
PROGRAM-ID. PRIME.
AUTHOR. SURYA.
INSTALLATION. PEARSON-PVT-LTD.
DATE-WRITTEN. 20/01/2016.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
OBJECT-COMPUTER. IBM-AS400.
SOURCE-COMPUTER. IBM-AS400.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP-VAL PIC 9(3).
01 WRK-VAL PIC 9(3).
01 I PIC 9(3).
01 QUT PIC 9(2).
01 QUT1 PIC 9(2).
01 REM PIC 9(2).
01 FLAG PIC X(1).

PROCEDURE DIVISION.

STEP-1.
MOVE 40 TO INP-VAL.
DISPLAY "Please find the prime numbers below:".
IF INP-VAL > 2 THEN DISPLAY "02".
PERFORM STEP-2 VARYING WRK-VAL FROM 3 BY 1 UNTIL WRK-VAL > INP-VAL.
STOP RUN.

STEP-2.
MOVE "Y" TO FLAG.
INITIALIZE REM, QUT, QUT1.
DIVIDE WRK-VAL BY 2 GIVING QUT.
PERFORM STEP-3 VARYING I FROM 2 BY 1 UNTIL I > QUT.
IF FLAG = 'Y' THEN DISPLAY WRK-VAL.

STEP-3.
INITIALIZE REM, QUT1.
DIVIDE WRK-VAL BY I GIVING QUT1 REMAINDER REM.
IF REM = 0 THEN MOVE "N" TO FLAG.

Is This Answer Correct ?    7 Yes 2 No

write a cobol program to display prime numbers between 1 to 100?..

Answer / amit mahajan

WORKING-STORAGE SECTION.
01 COUNTER PIC 99 VALUE ZEROES.
01 MAXLIMIT PIC 999 VALUE 100.
01 REM PIC 99 VALUE ZEROES.
01 QNT PIC 99 VALUE ZEROES.
01 NUM PIC 99 VALUE ZEROES.
01 I PIC 99.

PROCEDURE DIVISION.

PERFORM VARYING COUNTER FROM 2 BY 1 UNTIL COUNTER>=MAXLIMIT

MOVE 0 TO I

PERFORM VARYING NUM FROM 1 BY 1 UNTIL NUM > COUNTER
DIVIDE NUM INTO COUNTER GIVING QNT REMAINDER REM

IF REM=0
ADD 1 TO I
END-IF
END-PERFORM

IF I=2
DISPLAY COUNTER
END-IF
END-PERFORM

GOBACK.

Is This Answer Correct ?    6 Yes 4 No

write a cobol program to display prime numbers between 1 to 100?..

Answer / d karthik

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 i pic 9(3).
01 j pic 9(3).
01 c pic 9(2).
01 d pic 9(2).
01 r pic 9(2).
01 n pic 9(3).
PROCEDURE DIVISION.
accept n.
perform varying i from 1 by 1 until i > n
move 0 to c
perform varying j from 1 by 1 until j > i
divide i by j giving d remainder r
if r = 0
add 1 to c
end-if
end-perform
if c = 2
display i
end-if
end-perform.


STOP RUN.

Is This Answer Correct ?    5 Yes 6 No

write a cobol program to display prime numbers between 1 to 100?..

Answer / satyabrata nanda

Logic:
01 Var.
05 VarX Pic 9(2) Value zeros.
05 QNT Pic 9(2) value zeros.
05 REM Pic 9(2) value zeros.
05 CNT Pic 9(2) value zeros.
Procedure Div.

Display ' Enter VarX = '
Accept VarX.
If VarX = '1'
Display 'Prime no'
Else
Perform varying CNT from 2 by 1 until CNT >= VarX
Divide CNT into VarX giving QNT reminder REM
If rem > zero
Display 'Prime No'
Else
Display 'Not Prime'
End-if
End-if.

Is This Answer Correct ?    4 Yes 8 No

Post New Answer

More COBOL400 Interview Questions

When search all is used in cobol program without sorted input data

3 Answers  


what is the difference between SEARCH and SEARCHALL?

2 Answers  


define comp?

2 Answers  


What are the types of perform?

0 Answers  


What is sort? And its syntax?

0 Answers  






Define redefine?

0 Answers  


What are fillers? What is the actual use of fillers? With mall/simple example?

0 Answers  


Explain the input procedure and output procedure?

0 Answers  


can i anyone tell me how to use copybooks in cobol

1 Answers  


It's possible use %TRIM statement like rpgile?

2 Answers  


how array can be declare in cobol?

6 Answers  


why icetool be used in programmer view?

0 Answers  


Categories