Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Thursday, December 8, 2022

QBASIC program to check whether the input number is prime or composite

QBASIC program to check whether the input number is prime or composite


 CLS

INPUT "Enter any number:"; n

FOR x = 1 TO n

IF n MOD x = 0 THEN c = c + 1

NEXT x

IF c = 2 THEN

PRINT n; "is a prime number"

ELSE

PRINT n; "is composite number"

END IF

END

 

By using SUB procedure (SUB..END SUB)

DECLARE SUB prime (n)

CLS

INPUT "Enter any number:"; n

CALL prime(n)

END

 

SUB prime (n)

FOR x = 1 TO n

IF n MOD x = 0 THEN c = c + 1

NEXT x

IF c = 2 THEN

PRINT n; "is a prime number"

ELSE

PRINT n; "is composite number"

END IF

END SUB

 

By using FUNCTION procedure (FUNCTION..END FUNCTION)

DECLARE FUNCTION prime$ (n)

CLS

INPUT "Enter any number:"; n

PRINT n; prime$(n)

END

 

FUNCTION prime$ (n)

FOR x = 1 TO n

IF n MOD x = 0 THEN c = c + 1

NEXT x

IF c = 2 THEN

prime$ = "is a prime number"

ELSE

prime$ = "is composite number"

END IF

END FUNCTION


Output:



No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages