PRIME AND COMPOSITE QBASIC PROGRAMS



126. 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


127.   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

128.   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 <= 15 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 <= 15 THEN GOTO TOP
END SUB

129. 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

130.   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

Comments

Dina Thapa said…
please subscribe my channel
Anonymous said…
Write a program to evaluate gross salary for basic pay typed in through keyboard with house rent 30% of basic pay and dearness allowance 50% of basic pay
Unknown said…
WAP to find first composite number

Unknown said…
Did you hear there's a 12 word phrase you can communicate to your partner... that will induce deep emotions of love and instinctual attraction to you buried inside his chest?

Because deep inside these 12 words is a "secret signal" that fuels a man's instinct to love, admire and look after you with all his heart...

12 Words Will Trigger A Man's Desire Instinct

This instinct is so built-in to a man's brain that it will drive him to try better than before to make your relationship as strong as it can be.

In fact, fueling this influential instinct is absolutely important to achieving the best possible relationship with your man that the instance you send your man a "Secret Signal"...

...You will instantly find him expose his mind and heart for you in a way he never experienced before and he'll see you as the one and only woman in the universe who has ever truly appealed to him.
Hems Bhandari said…
Can you do it using select case?
If you can, please reply me!
Unknown said…
FIRST ONE 1 IS NOT PRIME
Unknown said…
using read datA

Popular posts from this blog

PROGRAM TO FIND SQUARE, CUBE, SQUARE ROOT AND CUBE ROOT OF GIVEN NUMBER

PROGRAM TO DISPLAY SUM, PRODUCT, DIFFERENCE AND PRODUCT OF TWO / THREE NUMBERS

PALINDROME AND ARMSTRONG PROGRAMS