Saturday, October 22, 2016

Qbasic Program: Factor and Factorial



137. WAP to input any number and display the factors.
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "FACTORS OF"; N; "=";
FOR I = 1 TO N
IF N MOD I = 0 THEN PRINT I;
NEXT I
END

USING SUB PROCEDURE

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

SUB FACT (N)
PRINT "FACTORS OF"; N; "=";
FOR I = 1 TO N
IF N MOD I = 0 THEN PRINT I;
NEXT I
END SUB


138. WAP to input any number and display the prime factors.

CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "PRIME FACTORS OF"; N; "=";
FOR I = 1 TO N
C = 0
FOR J = 1 TO I
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF N MOD I = 0 AND C = 2 THEN PRINT I;
NEXT I
END

USING SUB PROCEDURE

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

SUB FACT (N)
PRINT "PRIME FACTORS OF"; N; "=";
FOR I = 1 TO N
C = 0
FOR J = 1 TO I
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF N MOD I = 0 AND C = 2 THEN PRINT I;
NEXT I
END SUB

139. WAP to input any number and find sum of factors.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN S = S + I
NEXT I
PRINT "SUM OF FACTORS="; S
END
USING SUB PROCEDURE

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





SUB FACT (N)
S = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN S = S + I
NEXT I
PRINT "SUM OF FACTORS="; S
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION FACT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "SUM OF FACTORS=";  FACT (N)
END

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

140. WAP to input any number and display the factorial of a given number.
CLS
INPUT "ENTER ANY NUMBER"; N
F = 1
FOR I = 1 TO N
F = F * I
NEXT I
PRINT "FACTORIAL ="; F
END







USING SUB PROCEDURE

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

SUB FACT (N)
F = 1
FOR I = 1 TO N
F = F * I
NEXT I
PRINT "FACTORIAL ="; F
END SUB

USING FUNCTION PROCEDURE

DECLARE FUNCTION FACT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "FACTORIAL ="; FACT (N)
END

FUNCTION FACT (N)
F = 1
FOR I = 1 TO N
F = F * I
NEXT I
FACT = F
END FUNCTION









141. WAP to input any number and display the prime factorial of a given number.

CLS
INPUT "ENTER ANY NUMBER"; N
F = 1
FOR I = 1 TO N
C = 0
FOR J = 1 TO I
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN F = F * I
NEXT I
PRINT "PRIME FACTORIAL ="; F
END

USING SUB PROCEDURE

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

SUB FACT (N)
F = 1
FOR I = 1 TO N
C = 0
FOR J = 1 TO I
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN F = F * I
NEXT I
PRINT "PRIME FACTORIAL ="; F
END SUB






USING FUNCTION PROCEDURE

DECLARE FUNCTION FACT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "PRIME FACTORIAL ="; FACT (N)
END

FUNCTION FACT (N)
F = 1
FOR I = 1 TO N
C = 0
FOR J = 1 TO I
IF I MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN F = F * I
NEXT I
FACT = F
END FUNCTION

3 comments:

  1. WAP to input any number to display only the even factors of the supplied numbers by using SUB....END SUB

    ReplyDelete
  2. can you do a factorial number in while wend loop and do loop please

    ReplyDelete