我需要找到1到1000之间的所有素数,测试用例为: -
输入= 15.2
输出=请输入有效数字
输入= AAAAA
输出=请提供数值
输入= AAAAA15
输出=请提供数值
输入= 13
输出=非素数
输入= 25
输出= Prime
可以输入从-1到无穷大的整数
a)无法输入-2
b)可以输入-1
c)可以输入零(0)
d)可以输入最大的整数计算机可以处理
使用此代码,我能够实现测试用例4& 5,但无法找到其他测试用例的解决方案。
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3) VALUE ZERO. PROCEDURE DIVISION. MAIN-PARA.
PERFORM ACCEPT-PARA.
PERFORM PROCESS-PARA.
PERFORM DISPLAY-PARA.
STOP RUN.
ACCEPT-PARA.
DISPLAY 'ENTER N VALUE'.
ACCEPT N.
PROCESS-PARA.
MOVE 2 TO I.
PERFORM UNTIL I >= N
DIVIDE N BY I GIVING Q REMAINDER REM
IF REM = 0 THEN
DISPLAY 'GIVEN NUMBER IS NOT PRIME'
STOP RUN
END-IF
ADD 1 TO I
END-PERFORM.
DISPLAY-PARA.
IF N = I THEN
DISPLAY 'GIVEN NUMBER IS PRIME'
END-IF.
答案 0 :(得分:0)
IDENTIFICATION DIVISION.
PROGRAM-ID. PGM1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3)V99 VALUE ZERO.
77 I PIC 9(3) VALUE ZERO.
77 REM PIC 9(3) VALUE ZERO.
77 Q PIC 9 VALUE ZERO.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ACCEPT-PARA.
PERFORM PROCESS-PARA.
PERFORM DISPLAY-PARA.
STOP RUN.
ACCEPT-PARA.
DISPLAY " ENTER N VALUE : ".
ACCEPT N.
IF N IS ALPHABETIC
DISPLAY N " IS ALPHABETIC : PLEASE ENTER NUMERIC VALUE "
ELSE IF N IS NUMERIC
DISPLAY N " IS NUMERIC "
ELSE
DISPLAY N " IS ALPHANUMERIC : PLEASE ENTER NUMERIC VALUE "
END-IF.
PROCESS-PARA.
MOVE 2 TO I.
PERFORM UNTIL I >= N
DIVIDE N BY I GIVING Q REMAINDER REM
IF REM = 0 THEN
DISPLAY 'GIVEN NUMBER IS NOT PRIME'
STOP RUN
END-IF
ADD 1 TO I
END-PERFORM.
DISPLAY-PARA.
IF N = I THEN
DISPLAY 'GIVEN NUMBER IS PRIME'
END-IF.
enter code here
答案 1 :(得分:0)
IDENTIFICATION DIVISION.
PROGRAM-ID. PRIMENO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3).
01 NUM1 PIC 9(3).
01 R PIC 9(3).
01 Q PIC 9(3).
PROCEDURE DIVISION.
DISPLAY "ENTER N VALUE".
ACCEPT NUM.
PERFORM VARYING NUM1 FROM 2 BY 1
UNTIL NUM1 GREATER THAN OR EQUAL TO NUM
DIVIDE NUM BY NUM1 GIVING Q REMAINDER R
END-PERFORM.
IF R=0
DISPLAY NUM "not PRIME"
ELSE
DISPLAY NUM " PRIME"
END-IF.
STOP RUN.