To check the number prime or composite - (Qbasic Code)

To check the number prime or composite - (Qbasic Code)
Method I
 CLS
INPUT "Enter any number"; n
a = 0
FOR i = 1 TO n
    IF n MOD i = 0 THEN a = a + 1
NEXT i
IF a = 2 THEN
    PRINT "It is prime number";
ELSE
    PRINT "It is not prime number";
END IF
END


Method 2

CLS
 INPUT "ENTER A NUMBER"; N
FOR I = 2 TO N / 2
    IF N MOD I = 0 THEN
        C = C + 2
    END IF
NEXT I
IF C > 0 THEN
    PRINT "IT IS COMPOSITE"
ELSE
    PRINT "IT IS PRIME"
END IF
END


Using SUB Procedure
DECLARE SUB PRIME(N)
CLS
INPUT "ENTER A NUMBER"; N
CALL PRIME(N)
END
SUB PRIME (N)
FOR I = 2 TO N / 2
    IF N MOD I = 0 THEN
        C = C + 2
    END IF
NEXT I
IF C > 0 THEN
    PRINT "IT IS COMPOSITE"
ELSE
    PRINT "IT IS PRIME"
END IF
END SUB


Using Function Procedure
DECLARE FUNCTION PRIME (N)
CLS
INPUT "ENTER A NUMBER"; N
IF PRIME(N) > 0 THEN
    PRINT "IT IS COMPOSITE"
ELSE
    PRINT "IT IS PRIME"
END IF
END
FUNCTION PRIME (N)
FOR I = 2 TO N / 2
    IF N MOD I = 0 THEN
        C = C + 2
    END IF
NEXT I
PRIME = C
END FUNCTION

Share :

Twitter
Back To Top

facebook main

counter

Powered by Blogger.