Tuesday, September 26, 2017

Debug the Qbasic Programs [SEE Qbasic Practice PART III]

1.      DECLARE FUNCTION MAX(N ( ))
CLS
FOR I = 1 TO 10
INPUT A(I)
NEXT I
A = MAX(A ( ))
PRINT “The greatest number is”; X
END

FUNCTION MAX(N ( ))
FOR I = 1 TO 10
IF A(I) < H THEN H = A(I)
NEXT I
H = MAX
END FUNCTION

2.      REM TO FIND THE FACTORIAL OF GIVEN NUMBER
DECLARE FUNCTION FACT(N)
CLS
INPUT NUM
PRINT “The factorial is:”; FACT(N)
END

FUNCTION FACT(NUM)
F=2
WHILE N=0
F=F*N
N=N-1
WEND
F=FACT
END FUNCTION

3.      DECLARE FUNCTION dec(X)
INPUT”ENTER ANY BINARY NUMBER:’;n
PRINT”DECIMAL EQUIVALENT”;dec(n)
END

FUNCTION dec(n)
P=1
WHILE N<>0
R= N MOD 10
S=S+R*10^P
P=P+1
N=N/10
WEND
S=DEC
END FUNCTION

4.      DECLARE FACT (N)
CLS
REM PRINT FACTORS OF INPUT NUMBER WITH SUB PROCEDURE
INPUT “ENTER NUMBER:”, A
CALL FACT(N)
END

SUB FACT (N)
FOR K=1 TO N
IF N MOD K =1 THEN
PRINT K
IF END
END FUNCTION

5.      CLS
DECLARE FUNCTION factor(N)
INPUT “Enter a number”; X
LET S = factor(X)
PRINT “Frequency of factor is” ; S
END Function

FUNCTION factor(N)
FOR I = 1 to N
G = N MOD I
IF G = 0 THEN
F=F+1
NEXT I
F = Factor
END FUNCTION

6.      REM to print odd numbers from 32 to 12
DECLARE SUB SHOW ( )
CLS
CALL SHOW ( )
END

SUB SHOW ( )
N = 12
WHILE N <= 32
IF N MOD 2 = 0 THEN PRINT N;
N = N-1
NEXT N
END SUB

7.      REM to remove odd digits from a supplied numbers
DECLARE FUNCTION remodd(n)
CLS
INPUT “any number”; a
PRINT “Even digits only”; remodd(n)
END

FUNCTION remodd(n)
p = 0
WHILE n < > 0
R = n MOD 10
IF r MOD 2 = 1 THEN
s = s + r ^ p
p = p * 10
END IF
N = n \ 10
WEND
S = remodd
END FUNCTION


8.      DECLARE SUB TOTAL(P, D$)
CLS
INPUT "ENTER NO. OF PASSENGER"; P$
INPUT "ENTER DESTINATIONS"; D$
PRINT TOTAL (P, D$)
END

SUB TOTAL (P, D$)
IF UCASE$(D$) IS EQUAL TO "POKHARA" THEN
T = P * 450
ELSEIF UCASE$(D$) = "BUTWAL" THEN
T = P * 500
ELSEIF UCASE$(D$) = "CHITWAN" THEN
T = P * 300
END IF
IF P >= 5 THEN D = 5 / 100 * T
F = T - D
DISPLAY "TOTAL BUS FARE="; F
END SUB

9.      DECLARE SUB CHECK(N)
CLS
INPUT”ENTER A NUMBER”;NUM
C=CHECK(N)
END

SUB CHECK(A)
FOR I=2 TO N/2
IF N MOD 1=0 THEN
C=C+2
END IF
WEND
IF C>0 THEN
PRINT”IT IS COMPOSITE”
ELSE
PRINT”IT IS PRIME”
END IF
END SUB
10.   DECLARE FUNCTION CHECK(N)
CLS
INPUT”ENTER A NUMBER”;NUM
IF AB(N)>0 THEN
PRINT”IT IS COMPOSITE”
ELSE
PRINT”IT IS PRIME”
END IF
END

FUNCTION CHECK(A)
FOR I=2 TO N/2
IF  N MOD I=0 THEN
C=C+2
END IF
WEND
CHECK=C
END FUNCTION
11.   DECLARE FUNCTION CHECK$ (n)
CLS
ACCEPT num
C = check$ (num)
PRINT c$
End

FUNCTOIN CHECK$(n)
WHILE cnt = 1 to n
If n mod cnt = 0 then
f = f + 1
End if
Next cnt
If f = 1 then
Check$ = “prime”
ELSE
Check$ = “composite”
END IF
END FUNCTION

12.   REM to check the number is prime or not
DECLARE SUB CHECK(N)
CLS
INPUT N
CALL CHECK(N)
END

SUB CHECK(N)
I=3
DO
IF N / I = 0 THEN
PRINT “PRIME”
END
END IF
I = I – 1
LOOP UNTIL I < N – 1
PRINT “NOT PRIME”
END SUB

13.   REM to check whether the supplied no is odd or even
DECLARE SUB TEST(N)
CLS
INPUT”ENTER THE NUMBER;”;N$
CALL TEST(N)
END

SUB TEST(N)
Y=2
R=Y MOD N
IF R=1 THEN
PRINT”EVEN”
OR
PRINT”ODD”
END
END SUB

14.   REM to check whether the given number is odd or even
DECLARE SUB CHECK (nl)
CLS
INPUT “ENTER THE NUIMBER:” ; nl
CALL sub (nl)
END

SUB CHECK (nl)
Y = 2
r = Y MOD nl
IF R = 0 THEN
PRINT “EVEN NUMBER”
OR
PRINT “ODD NUMBER”
END
END SUB

15.   CLS
DECLARE FUNCTION OE$(N)
INPUT " ENTER A NUMBER:"; X
ANS= OE$(X)
PRINT ANS
END

FUNCTION OE$(N)
R= N MOD 2
IF R = 0 THEN
X$= "EVEN"
ELSE
X$="ODD"
END IF
X$=OE$
END SUB

16.   REM to find whether the number is odd or even
DECLARE SUB SUM$ (A)
CLS
INPUT “range” ; A
Call SUM$
END

SUB SUM(A)
IF A mod 2=0 than print n$=”even” else print “odd”
END FUNCTION

17.   DECLARE FUNCTION TEST$(N)
REM to test user entered number is even or odd.
CLS
INPUT “ENTER A NUMBER”;A
CALL TEST$(A)
END

FUNCTION TEST (N)
IF N MOD 2 IS ZERO THEN
T$= “EVEN”
ELSE
T$= “ODD”
ENDIF
TEST=T$
END FUNCTION

18.   DECLARE FUNCTION DIVISIBLE$(N)
CLS
REM CHECK WHETHER NUMBER IS DIVISIBLE BY 2 AND 3 REM OR NOT
INPUT “ENTER NUMBER:”, A
S$ = DIVISIBLE$ ( A )
PRINT “Number is “; S$
END

FUNCTION DIVISIBLE$ (N)
R = N \ 2
R1= N MOD 3
IF R= 0 AND R1 = 1 THEN
"DIVISIBLE" = DIVISIBLE$
ELSE
DIVISIBLE$ = "NOT DIVISIBLE"
END IF
END FUNCTION

19.   DECLARE FUNCTION CHECK( N)
CLS
INPUT “Enter any number”; N$
PRINT CHECK$(N)
END

FUNCTION CHECK(N)
IF N = 0 THEN
CHECK$ = “POSITIVE”
ELSEIF N < 0 THEN
PRINT “NEGATIVE”
END FUNCTION


20.   DECLARE FUNCTION userlen (a$)
REM length of sting without LEN function
CLS
INPUT “Type any string value: ”; b$
PRINT “Length of the string: “; userlen(a$)
END

FUNCTION userlen (a$)
c=1
aa:
b$ = MID$(a$, p, 1)
IF b$ = “THEN”
p=p+1
c=c+1
GOTO aa
END IF
userlen = c
END FUNCTION

4 comments: