Sunday 15 March 2020

QBASIC PROGRAM USING SUB Procedure


a. WAP PROGRAM TO DISPLAY AREA OF TRIANGLE
DECLARE SUB AREA (B, H)
CLS
INPUT “ENTER BREADTH”; B
INPUT “ENTER HEIGHT”; H
CALL AREA (B, H)
END

SUB AREA (B, H)
A = 1 / 2  * (B * H)
PRINT “AREA OF TRIANGLE”; A
END SUB

d. Acceleration of car
Declare sub acc(vi,vf,ti,tf)
CLS
INPUT "enter initial velocity"; vi
INPUT "enter final velocity"; vf
INPUT "enter initial time"; ti
INPUT "enter final time"; tf
CALL acc(vi, vf, ti, tf)
END

SUB acc (vi, vf, ti, tf)
    v = vf - vi
    t = tf - ti
    accl = v / t
    PRINT v
    PRINT t
    PRINT "acceleration is"; accl
END SUB

e. WAP to input any number and check whether the given no. is positive, negative or zero.
DECLARE SUB CHECK (N)
CLS
INPUT “ENTER ANY NUMBER”; N
CALL CHECK (N)
END
SUB CHECK (N)
IF N > 0 THEN
PRINT N; IS POSITIVE NUMBER”
ELSEIF N < 0 THEN
PRINT N; IS NEGATIVE NUMBER”
ELSE
PRINT N; IS ZERO”
END IF
END SUB

f. WAP to input any number and display whether it is odd or even.
DECLARE SUB CHECK (N)
CLS
INPUT “ENTER ANY NUMBER”; N
CALL CHECK (N)
END

SUB CHECK (N)
IF N MOD 2 = 0 THEN
      PRINT N; “IS EVEN NUMBER”
ELSE
PRINT N; “IS ODD NUMBER”
END IF
END SUB

g. Triangle can be formed or not from given 3 sides
Declare sub tri(a,b,c)
CLS
INPUT "first side length"; a
INPUT "second side length"; b
INPUT "third side length"; c
CALL tri(a, b, c)
END

SUB tri (a, b, c)
    IF (a + b > c) THEN
        PRINT "triangle can be formed"
    ELSE
        PRINT "triangle cannot be formed"
    END IF
END SUB

h. Print first 10 natural number and its square and cube
Declare sub num()
CLS
CALL num
END

SUB num ()
    FOR p = 1 TO 10
        PRINT p
        a = p ^ 2
        PRINT a
        b = p ^ 3
        PRINT b
    NEXT p
END SUB

m.  To find leap year.
CLS
INPUT "ENTER YEAR"; Y
IF Y MOD 4 = 0 AND Y MOD 100 <> 0 OR Y MOD 400 = 0 THEN
    PRINT Y; "IS LEAP YEAR"
ELSE
    PRINT Y; "IS NOT LEAP YEAR"
END IF
END

O. check whether the given no. is prime or composite.
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

p. Prime number between 2 to 200
DECLARE SUB PRIME ( )
CLS
CALL PRIME
END

SUB PRIME
    FOR N = 2 TO 200
        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

q. find sum from 1 to n number. Eg. 1+2+3+….+N
declare sub summ(n)
CLS
    INPUT "Enter a number"; n
CALL summ(n)
END

SUB summ (n)
    FOR p = 1 TO n
        sum = sum + p
    NEXT p
    PRINT sum
END SUB
r. Factorial number
Declare sub fac(n,f)
CLS
INPUT "ENTER A NUMBER:", N
F = 1
CALL fac(N, F)
END

SUB fac (n, f)
    FOR I = 1 TO n
        f = f * I
    NEXT I
    PRINT "THE FACTORIAL IS", f
END SUB

t. to check input is either number, letter or symbol
declare sub check(ui$)
CLS
INPUT "Enter String To Separate: ", UI$
CALL check(UI$)
END

SUB check (ui$)
    FOR x = 1 TO LEN(ui$)
        TC$ = MID$(ui$, x, 1)

        IF (ASC(TC$) > 64 AND ASC(TC$) < 91) OR (ASC(TC$) > 96 AND ASC(TC$) < 123) THEN
            LC$ = LC$ + TC$
        ELSEIF ASC(TC$) > 47 AND ASC(TC$) < 58 THEN
            NC$ = NC$ + TC$
        ELSE
            OC$ = OC$ + TC$
        END IF
    NEXT x

    PRINT
    PRINT "Letters: "; LC$
    PRINT "Numbers: "; NC$
    PRINT "Other: "; OC$
END SUB

No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint