Prime Composite Testing Program Using User Defined Function

DECLARE FUCTION ISPRIME(N)
CLS
INPUT "Testing Number: ";N
IF ISPRIME(N)=1 THEN
    PRINT N; " IS PRIME NUMBER"
ELSE
   PRINT N;" IS COMPOSITE NUMBER"
ENDIF

FUNCTION ISPRIME(N)
'F if flag which is set to 1 means it is assumed to be prime at first
F=1
'if there is any factor from 2 to half of the number n then the number is composite
FOR I= 2 TO N/2
    IF N MOD I=0 THEN
        F=0
        EXIT FOR
     ENDIF
NEXT I
ISPRIME=F
END FUNCTION

No comments:

Post a Comment