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