Saturday, October 22, 2016

Qbasic Program: Prime or Composite



132. WAP to input number and check whether the given no. is prime or composite.

CLS

INPUT "ENTER ANY NUMBER"; N
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS COMPOSITE NUMBER"
END IF
END

USING SUB PROCEDURE

DECLARE SUB PRIME (N)
INPUT "ENTER ANY NUMBER"; N
CALL PRIME (N)
END

SUB PRIME (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS COMPOSITE NUMBER"
END IF
END SUB






USING FUNCTION PROCEDURE

DECLARE FUNCTION PRIME(N)
CLS
INPUT "ENTER ANY NUMBER"; N
P = PRIME (N)
IF P = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS COMPOSITE NUMBER"
END IF
END

FUNCTION PRIME (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
PRIME = C
END FUNCTION


133.   WAP to display all prime numbers from 1 to 100.

CLS

FOR N = 1 TO 100
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN PRINT N,
NEXT N
END






USING SUB PROCEDURE

DECLARE SUB PRIME ( )
CLS
CALL PRIME
END

SUB PRIME
FOR N = 1 TO 100
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN PRINT N,
NEXT N
END SUB

134.   WAP to display first 15 prime numbers
CLS

N = 1
CNT = 1
TOP:
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N,
CNT = CNT + 1
END IF
N = N + 1
IF CNT <= 20 THEN GOTO TOP
END







USING SUB PROCEDURE

DECLARE SUB PRIME ( )
CLS
CALL PRIME
END

SUB PRIME
N = 1
CNT = 1
TOP:
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N,
CNT = CNT + 1
END IF
N = N + 1
IF CNT <= 20 THEN GOTO TOP
END SUB

135. WAP to input number and check whether the given no. is prime or not.

CLS

INPUT "ENTER ANY NUMBER"; N
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS NOT PRIME NUMBER"
END IF
END


USING SUB PROCEDURE

DECLARE SUB PRIME (N)
INPUT "ENTER ANY NUMBER"; N
CALL PRIME (N)
END

SUB PRIME (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS NOT PRIME NUMBER"
END IF
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION PRIME(N)
CLS
INPUT "ENTER ANY NUMBER"; N
P = PRIME (N)
IF P = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS NOT PRIME NUMBER"
END IF
END

FUNCTION PRIME (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
PRIME = C
END FUNCTION

136.   WAP to input number and check whether the given no. composite or not.
CLS

INPUT "ENTER ANY NUMBER"; N
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C < > 2 THEN
PRINT N; "IS COMPOSITE NUMBER"
ELSE
PRINT N; "IS NOT COMPOSITE NUMBER"
END IF
END

USING SUB PROCEDURE

DECLARE SUB COMPOSITE (N)
INPUT "ENTER ANY NUMBER"; N
CALL COMPOSITE (N)
END

SUB COMPOSITE (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C < > 2 THEN
PRINT N; "IS COMPOSITE NUMBER"
ELSE
PRINT N; "IS NOT COMPOSITE NUMBER"
END IF
END SUB







USING FUNCTION PROCEDURE

DECLARE FUNCTION COMPOSITE (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CO = COMPOSITE (N)
IF CO < > 2 THEN
PRINT N; "IS COMPOSITE NUMBER"
ELSE
PRINT N; "IS NOT COMPOSITE NUMBER"
END IF
END

FUNCTION COMPOSITE (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
COMPOSITE = C
END FUNCTION



No comments:

Post a Comment