QBASIC Modular Programming of Sunrise Publication



Review of Control Statements and Library Functions used in QBASIC.

1. Write a program to print all the odd numbers between 1 to 50.
CLS
O = 1
FOR I = 1 TO 50
    PRINT O;
    O = O + 2
NEXT I
END

2. Write a program to print all the even numbers between 2 to 50 in the descending order.
CLS
E = 50
FOR I = 50 TO 2 STEP -2
    PRINT E;
    E = E - 2
NEXT I
END

3. Write a program to print the multiplication table of given number up to 10th terms.
CLS
INPUT "ENTER ANY NUMBERS"; N
FOR I = 1 TO 10
    PRINT N; "X"; I; "="; N * I
NEXT I
END

4. Write a program to input any 5 numbers and print them on ascending order.
CLS
DIM N(5)
FOR I = 1 TO 5
    INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
FOR I = 1 TO 5
    FOR J = 1 TO 5 - I
        IF N(J) > N(J + 1) THEN SWAP N(J), N(J + 1)
    NEXT J
NEXT I
PRINT "NUMBERS ARRANGED IN ASCENDING ORDER"
FOR I = 1 TO 5
    PRINT N(I)
NEXT I
END

5. Write a program to input a number and find factorial of that number.
CLS
F = 1
INPUT "ENTER ANY NUMBER"; N
FOR I = 1 TO N
    F = F * I
NEXT I
PRINT "FACTORIAL NUMBER = "; F
END

6. Write a program to accept three numbers and determine the largest one.
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
IF A > B AND A > C THEN
    PRINT A; "IS THE LARGEST ONE"
ELSEIF B > A AND B > C THEN
    PRINT B; "IS THE LARGEST ONE"
ELSE
    PRINT C; "IS THE LARGEST ONE"
END IF
END

7. Write a program to accept three numbers and determine the smallest one.
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
IF A < B AND A < C THEN
    PRINT A; "IS THE SMALLEST ONE"
ELSEIF B < A AND B < C THEN
    PRINT B; "IS THE SMALLEST ONE"
ELSE
    PRINT C; "IS THE SMALLEST ONE"
END IF
END

8. Write a program to accept three numbers and determine the medium number.
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
IF A > B AND A < C OR A < B AND A > C THEN
    PRINT A; "IS MEDIUM NUMBER"
ELSEIF B > A AND B < C OR B < A AND B > C THEN
    PRINT B; "IS MEDIUM NUMBER"
ELSE
    PRINT C; "IS MIDDLE NUMBER"
END IF
END

9. Write a program to check and print whether the entered number is primer or composite.
CLS
INPUT "ENTER ANY NUMBER"; N
FOR I = 1 TO N
    IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
    PRINT "PRIME NUMBER"
ELSE
    PRINT "COMPOSITE NUMBER"
END IF
END

10. Write a program to check and print whether the entered number is Armstrong or not.
CLS
INPUT "ENTER ANY NUMBER"; N
A = N: S = 0
WHILE N <> 0
    R = N MOD 10
    S = S + R ^ 3
    N = N \ 10
WEND
IF A = S THEN
    PRINT "ARMSTRONG"
ELSE
    PRINT "NOT ARMSTRONG"
END IF
END

11. Write a program to check and print whether the entered number is palindrome or not.
CLS
INPUT "ENTER ANY NUMBER"; N
A = N: S = 0
WHILE N <> 0
    R = N MOD 10
    S = S * 10 + R
    N = N \ 10
WEND
IF A = S THEN
    PRINT "PALINDROME"
ELSE
    PRINT "NOT PALINDROME"
END IF
END

12. Write a program to check and print whether the entered word is palindrome or not.
CLS
INPUT "ENTER ANY WORD"; A$
FOR I = LEN(A$) TO 1 STEP -1
    R$ = R$ + MID$(A$, I, 1)
NEXT I
IF R$ = A$ THEN
    PRINT "PALINDROME"
ELSE
    PRINT "NOT PALINDROME"
END IF
END

13. Write a program to check and print whether the entered number is divisible by 5 and 9 or not.
CLS
INPUT "ENTER ANY NUMBER"; N
IF N MOD 5 = 0 AND N MOD 9 = 0 THEN
    PRINT "DIVISIBLE BY 5 AND 9"
ELSE
    PRINT "NOT DIVSIBLE BY 5 AND 9"
END IF
END

14. Write a program to convert decimal number into binary equivalent number.
CLS
INPUT "ENTER ANY NUMBER"; N
WHILE N <> 0
    R = N MOD 2
    S$ = STR$(R) + S$
    N = N \ 2
WEND
PRINT "BINARY NUMBER = "; S$
END

15. Write a program to convert binary number to decimal equivalent number.
CLS
INPUT "ENTER BINARY NUMBER"; N$
FOR I = LEN(N$) TO 1 STEP -1
    B$ = MID$(N$, I, 1)
    S = S + VAL(B$) * 2 ^ P
    P = P + 1
NEXT I
PRINT "DECIMAL EQUIVALENT VALUE = "; S
END

16. Write a program to check an input number is positive or negative.
CLS
INPUT "ENTER ANY NUMBER"; N
IF N > 0 THEN
    PRINT N; "IS POSITIVE NUMBER"
ELSE
    PRINT N; "IS NEGATIVE NUMBER"
END IF
END

 17. Write a program to input a string then print it in the reverse order.
CLS
INPUT "ENTER ANY WORD"; A$
FOR I = LEN(A$) TO 1 STEP -1
    R$ = R$ + MID$(A$, I, 1)
NEXT I
PRINT R$
END

18. Write a program to input a sentence then count total number of words in the sentence.
CLS
INPUT "ENTER ANY WORDS"; A$
FOR I = 1 TO LEN(A$)
    B$ = MID$(A$, I, 1)
    C = C + 1
NEXT I
PRINT "TOTAL NO OF WORDS = "; C
END

19. Write a program to check whether an entered word is palindrome or not.
CLS
INPUT "ENTER ANY NUMBER"; N
A = N
S = 0
WHILE N <> 0
    R = N MOD 10
    S = S * 10 + R
    N = N \ 10
WEND
IF A = S THEN
    PRINT "PALINDROME"
ELSE
    PRINT "NOT PALINDROME"
END IF
END

20. Write a program to input word and count total number of vowels and consonants.
CLS
A$ = "KATHMANDU"
FOR I = 1 TO LEN(A$)
    B$ = MID$(A$, I, 1)
    IF B$ = "A" OR B$ = "E" OR B$ = "I" OR B$ = "O" OR B$ = "U" THEN V = V + 1
    IF B$ <> "A" AND B$ <> "E" AND B$ <> "I" AND B$ <> "O" AND B$ <> "U" THEN C = C + 1
NEXT I
PRINT "TOTAL NO OF VOWELS = "; V
PRINT "TOTAL NO OF CONSONANTS = "; C
END

Write programs to display the following numeric series.

1. 1, 4, 9, 16, 25……100
CLS
FOR I = 1 TO 100
    PRINT I ^ 2;
NEXT I
END

2. 1, 2, 4, 7, 11….10th term
1st
CLS
A = 1
B = 1
FOR I = 1 TO 10
    PRINT A;
    A = A + B
    B = B + 1
NEXT I
END

2nd
CLS
A = 1
FOR I = 1 TO 10
    PRINT A;
    A = A + I
NEXT I
END

3. 100, 95, 90, 85….50
CLS
FOR I = 100 TO 50 STEP -5
    PRINT I;
NEXT I
END

4. 3, 33, 333, 3333, 33333
CLS
N = 3
FOR I = 1 TO 5
    PRINT N;
    N = N * 10 + 3
NEXT I
END

5. 1, 1, 2, 3, 5, 8….10th term
CLS
A = 1
B = 1
PRINT A; B;
FOR I = 1 TO 10
    C = A + B
    PRINT C;
    A = B
    B = C
NEXT I
END


Write a program to print the following numeric and string patterns.

1.        5
54
543
5432
54321
CLS
FOR I = 5 TO 1 STEP -1
    FOR J = 5 TO I STEP -1
        PRINT J;
    NEXT J
    PRINT
NEXT I
END

2.        1
12
123
1234
12345
CLS
FOR I = 1 TO 5
    FOR J = 1 TO I
        PRINT J;
    NEXT J
    PRINT
NEXT I
END

3.
1
22
333
4444
55555
CLS
FOR I = 1 TO 5
    FOR J = 1 TO I
        PRINT I;
    NEXT J
    PRINT
NEXT I
END

4.      NEPAL
            EPA
              P
CLS
A$ = "NEPAL"
B = 1: C = 5
FOR I = LEN(A$) TO 1 STEP -2
    PRINT TAB(C); MID$(A$, B, I)
    C = C + 1: B = B + 1
NEXT I
END

5.            *********
                *******
        *****
         ***
           *
CLS
A$ = "*********"
C = 9: B = 30
FOR I = 1 TO 6
    PRINT TAB(B); MID$(A$, I, C)
    C = C - 2
    B = B + 1
NEXT I
END

6.                   M
                   HMA
                THMAN
              ATHMAND
  KATHMANDU
CLS
A$ = "KATHMANDU"
C = 1: B = 35
FOR I = 5 TO 1 STEP -1
    PRINT TAB(B); MID$(A$, I, C)
    C = C + 2
    B = B - 1
NEXT I
END

7.                  *
        ***
      ******
     *******
   *********
CLS
A$ = "*********"
T = 10
FOR I = 1 TO 9 STEP 2
    PRINT TAB(T); LEFT$(A$, I)
    T = T - 1
NEXT I
END

Modular Programming in QBASIC

1. Write a program to declare a sub-procedure module to find the perimeter of a rectangle. [P=2(L+B)]
DECLARE SUB PERI (L,B)
CLS
INPUT "ENTER THE LENGTH OF A RECTANGLE"; L
INPUT "ENTER THE BREADTH OF A RECTANGLE"; B
CALL PERI(L, B)
END
SUB PERI (L, B)
    P = 2 * (L + B)
    PRINT "PERIMETER OF A RECTANGLE"; P
END SUB

2. Write a program to calculate the area of a circle. [PIR2]
DECLARE SUB AREA (R)
CLS
INPUT "ENTER RADIUS OF A CIRCLE"; R
CALL AREA(R)
END
SUB AREA (R)
    A = 3.14 * R ^ 2
    PRINT "AREA OF A CIRCLE"; A
END SUB

3. Write a program to convert meter into kilometer.
DECLARE SUB CONV (M)
CLS
INPUT "ENTER DISTANCE IN METER"; M
CALL CONV(M)
SUB CONV (M)
    K = M / 1000
    PRINT "METER INTO KILOMETER"; K
END SUB

4. Write a program to check and print whether entered number 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 "EVEN NUMBER"
    ELSE
        PRINT "ODD NUMBER"
    END IF
END SUB

5. Write a program to declare a sub-procedure module name (Z$) to print your name for 5 times.
DECLARE SUB DIS (Z$)
CLS
INPUT "ENTER YOUR NAME"; Z$
CALL DIS(Z$)
END
SUB DIS (Z$)
    FOR I = 1 TO 10
        PRINT Z$:
    NEXT I
END SUB

6. Write a program to declare a sub-procedure module to find the greatest among 3 different numbers, where the numbers are passed as parameters.
DECLARE SUB GREATEST (A,B,C)
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
CALL GREATEST(A, B, C)
END
SUB GREATEST (A, B, C)
    IF A > B AND A > C THEN
        PRINT A; "IS THE GREATEST NUMBER"
    ELSEIF B > A AND B > C THEN
        PRINT B; "IS THE GREATEST NUMBER"
    ELSE
        PRINT C; "IS THE GREATEST NUMBER"
    END IF
END SUB

7. Write a program to declare a sub procedure module to calculate cube of a number.
DECLARE SUB CUBE (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL CUBE(N)
END
SUB CUBE (N)
    C = N ^ 3
    PRINT "CUBE OF A NUMBER"; C
END SUB

8. Write a program to check and print whether the entered number is prime or composite.
DECLARE SUB CHECK (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL CHECK(N)
END
SUB CHECK (N)
    FOR I = 1 TO N
        IF N MOD I = 0 THEN C = C + 1
    NEXT I
    IF C = 2 THEN
        PRINT "PRIME NUMBER"
    ELSE
        PRINT "COMPOSITE NUMBER"
    END IF
END SUB

9. Write a program to find and print sum of 'N' entered numbers.
DECLARE SUB SUM (N)
CLS
INPUT "ENTER ANY NUMBERS"; N
CALL SUM(N)
END
SUB SUM (N)
    S = 0
    FOR I = 1 TO N
        S = S + I
    NEXT I
    PRINT "SUM = "; S
END SUB

10. Write a QBASIC program using SUB procedure PALINDROME (W$, N$) to check whether input word is palindrome or not. (Palindrome words are those words which spell the same from both the side).
DECLARE SUB CHECK (N$)
CLS
INPUT "ENTER ANY WORDS"; N$
CALL CHECK(N$)
END
SUB CHECK (N$)
    FOR I = LEN(N$) TO 1 STEP -1
        W$ = W$ + MID$(N$, I, 1)
    NEXT I
    IF W$ = N$ THEN
        PRINT "PALINDROME"
    ELSE
        PRINT "NOT PALINDROME"
    END IF
END SUB

11. Write a program to get a number from the user and then print the sum of the digits of the supplied number.
DECLARE SUB SUM (N)
CLS
INPUT "ENTER ANY DIGITS"; N
CALL SUM(N)
END
SUB SUM (N)
    S = 0
    WHILE N <> 0
        R = N MOD 10
        S = S + R
        N = N \ 10
    WEND
    PRINT "SUM = "; S
END SUB

12. Write a program to find the highest and lowest among 3 entered numbers using two different sub modules.
DECLARE SUB HIGHEST (A,B,C)
DECLARE SUB LOWEST (A,B,C)
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
CALL HIGHEST(A, B, C)
CALL LOWEST(A, B, C)
END
SUB HIGHEST (A, B, C)
    IF A > B AND A > C THEN
        PRINT A; "IS THE HIGHEST NUMBER"
    ELSEIF B > A AND B > C THEN
        PRINT B; "IS THE HIGHEST NUMBER"
    ELSE
        PRINT C; "IS THE HIGHEST NUMBER"
    END IF
END SUB
SUB LOWEST (A, B, C)
    IF A < B AND A < C THEN
        PRINT A; "IS THE LOWEST NUMBER"
    ELSEIF B < A AND B < C THEN
        PRINT B; "IS THE LOWEST NUMBER"
    ELSE
        PRINT C; "IS THE LOWEST NUMBER"
    END IF
END SUB

13. Write a program to display the multiplication table of entered number up to 10 multiples.
DECLARE SUB MULTIPLY (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL MULTIPLY(N)
END
SUB MULTIPLY (N)
    FOR I = 1 TO 10
        PRINT N; "X"; I; "="; N * I
    NEXT I
END SUB

14. Write a program to count number of consonants in user entered word.
DECLARE SUB COUNT (C$)
CLS
INPUT "ENTER ANY WORD"; C$
CALL COUNT(C$)
END
SUB COUNT (C$)
    FOR I = 1 TO LEN(C$)
        B$ = MID$(C$, I, 1)
        IF B$ <> "A" AND B$ <> "E" AND B$ <> "I" AND B$ <> "O" AND B$ <> "U" THEN
            C = C + 1
        END IF
    NEXT I
    PRINT "TOTAL NO OF CONSONANTS = "; C
END SUB

15. Write a program to calculate area to calculate area of cube where length is given. [A=6*l^2]
DECLARE SUB CUBE (L)
CLS
INPUT "ENTER LENGTH OF CUBE"; L
CALL CUBE(L)
END
SUB CUBE (L)
    AREA = 6 * L ^ 2
    PRINT "TOTAL AREA OF CUBE"; AREA
END SUB

16. Write a program to calculate compound interest for given principle, rate and time. [I=P * (1 + (R / 100)) ^ T-1]
DECLARE SUB COMPOUNDINTEREST(P,R,T)
CLS
INPUT "ENTER THE PRINCIPLE"; P
INPUT "ENTER THE RATE"; R
INPUT "ENTER THE TIME"; T
CALL COMPOUNDINTEREST(P, R, T)
END
SUB COMPOUNDINTEREST (P, R, T)
    CI = P * (1 + (R / 100)) ^ T - 1
    PRINT "TOTAL COMPOUND INTEREST"; CI
END SUB

17. Write a program to print only vowel characters by removing consonants in a user entered word.
DECLARE SUB VOWEL (A$)
CLS
INPUT "ENTER ANY WORD"; A$
CALL VOWEL(A$)
END
SUB VOWEL (A$)
    FOR I = 1 TO LEN(A$)
        B$ = MID$(A$, I, 1)
        IF B$ = "A" OR B$ = "E" OR B$ = "I" OR B$ = "O" OR B$ = "U" THEN
            V = V + 1
        END IF
    NEXT I
    PRINT "TOTAL NO OF VOWEL = "; V
END SUB

18. Write a program to input any string then display it in an alternative case. [For example if you enter "Sunrise" then it should be displayed as "sUnRiSe"]
DECLARE SUB ALT(S$)
CLS
INPUT "ENTER ANY WORD"; S$
CALL ALT(S$)
END
SUB ALT (S$)
    FOR I = 1 TO LEN(S$)
        B$ = MID$(S$, I, 1)
        IF I MOD 2 = 1 THEN
            W$ = W$ + LCASE$(B$)
        ELSE
            W$ = W$ + UCASE$(B$)
        END IF
    NEXT I
    PRINT W$
END SUB

19. Write a program to convert the entered binary number to equivalent octal number.
DECLARE SUB CONVERT (B$)
CLS
INPUT "ENTER BINARY NO"; B$
CALL CONVERT(B$)
END
SUB CONVERT (B$)
    FOR I = LEN(B$) TO 1 STEP -1
        D$ = MID$(B$, I, 1)
        D = VAL(D$) * 2 ^ A
        DEC = DEC + D
        A = A + 1
    NEXT I
    WHILE DEC <> 0
        P = DEC MOD 8
        DEC = DEC \ 8
        A$ = STR$(P) + A$
    WEND
    PRINT A$;
END SUB

20. Write a program to display all the composite numbers existing in between 1 to 100.
DECLARE SUB COMPOSITE ()
CLS
CALL COMPOSITE
END
SUB COMPOSITE ()
    FOR I = 1 TO 100
        C = 0
        FOR J = 1 TO I
            IF I MOD J = 0 THEN C = C + 1
        NEXT J
        IF C <> 2 THEN PRINT I;
    NEXT I
END SUB

21. Write a program to input any five numbers then arrange in descending order and display them horizontally.
DECLARE SUB DIS (N)
CLS
CALL DIS(N)
END
SUB DIS (N)
    DIM N(5)
    FOR I = 1 TO 5
        INPUT "ENTER THE NUMBERS"; N(I)
    NEXT I
    FOR I = 1 TO 5
        FOR J = 1 TO 5 - I
            IF N(J) < N(J + 1) THEN SWAP N(J), N(J + 1)
        NEXT J
    NEXT I
    PRINT "NUMBERS ARRANGED IN DESCENDING ORDER"
    FOR I = 1 TO 5
        PRINT N(I);
    NEXT I
END SUB

22. Write a program to input full name of person then print initial letters of first name and middle name but full name of last name i.e. caste. [For example: R.P. Aryal for Ram Prasad Aryal]
DECLARE SUB DIS (A$)
CLS
INPUT "ENTER FULL NAME"; A$
CALL DIS(A$)
END
SUB DIS (A$)
    B$ = LEFT$(A$, 1)
    FOR J = 1 TO LEN(A$)
        C$ = MID$(A$, J, 1)
        IF C$ = " " THEN B$ = B$ + "." + MID$(A$, J + 1, 1)
    NEXT J
    FOR I = LEN(A$) TO 1 STEP -1
        D$ = MID$(A$, I, 1)
        F = F + 1
        IF D$ = " " THEN
            E$ = E$ + RIGHT$(A$, F - 2)
            EXIT FOR
        END IF
    NEXT I
    PRINT B$ + E$
END SUB

23. Write a program to detect and print whether entered number is positive, negative or netural.
DECLARE SUB CHECK (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL CHECK(N)
END
SUB CHECK (N)
    IF N > 0 THEN
        PRINT "POSITIVE NUMBER"
    ELSEIF N < 0 THEN
        PRINT "NEGATIVE NUMBER"
    ELSE
        PRINT "NEUTRAL NUMBER"
    END IF
END SUB

24. Write a program to calculate sum and average or two fixed number i.e 165 and 265.
DECLARE SUB SUM (A)
DECLARE SUB AVERAGE (B)
CLS
INPUT "ENTER FIRST NUMBER"; A
INPUT "ENTER SECOND NUMBER"; B
CALL SUM(A)
CALL AVERAGE(B)
END
SUB SUM (A)
    S = 0
    WHILE A <> 0
        R = A MOD 10
        S = S + R
        A = A \ 10
    WEND
    PRINT "SUM = "; S
END SUB
SUB AVERAGE (B)
    WHILE B <> 0
        R = B MOD 10
        AVG = AVG + R / 3
        B = B \ 10
    WEND
    PRINT "AVERAGE = "; AVG
END SUB

25. Write a program to check whether given number is perfect cube or not.
DECLARE SUB PERFECT (N)
CLS
INPUT "ENTER A NUMBER"; N
CALL PERFECT(N)
END
SUB PERFECT (N)
    SR = N ^ (1 / 3)
    IF SR = INT(SR) THEN
        PRINT "IT IS PERFECT CUBE"
    ELSE
        PRINT "IT IS NOT PERFECT CUBE"
    END IF
END SUB

26. Write a program to determine if the input number is palindrome or not.
DECLARE SUB PALINDROME (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL PALINDROME(N)
SUB PALINDROME (N)
    A = N
    S = 0
    WHILE N <> 0
        R = N MOD 10
        S = S * 10 + R
        N = N \ 10
    WEND
    IF A = S THEN
        PRINT "PALINDROME"
    ELSE
        PRINT "NOT PALINDROME"
    END IF
END SUB

27. Write a program to count and print number of data divisible by 3 and 5 from the given set of data. [Data 5, 15, 25, 45, 18, 25, 60, 50]
DECLARE SUB DIVI (N)
CLS
CALL DIVI(N)
END
SUB DIVI (N)
    DIM N(8)
    FOR I = 1 TO 8
        INPUT ":"; N(I)
    NEXT I
    PRINT "DIVSIBLE BY 3 AND 5"
    FOR I = 1 TO 8
        IF N(I) MOD 3 = 0 AND N(I) MOD 5 = 0 THEN PRINT N(I)
    NEXT I
END SUB

28. Write a program to input length and breadth of rectangle then calculate its area and perimeter using two different sub modules.
DECLARE SUB AREA (L,B)
DECLARE SUB PERI (L,B)
CLS
INPUT "ENTER THE LENGTH"; L
INPUT "ENTER THE BREADTH"; B
CALL AREA(L, B)
CALL PERI(L, B)
END
SUB AREA (L, B)
    A = L * B
    PRINT "AREA OF RECTANGLE"; A
END SUB
SUB PERI (L, B)
    P = 2 * (L + B)
    PRINT "PERIMETER OF RECTANGLE"; P
END SUB

29. Write a program to check whether a person is eligible to caste vote or not according to enter age. [Note: A person aged 18 or more can vote]
DECLARE SUB CHECK (N)
CLS
INPUT "ENTER THE AGE OF PERSON"; N
CALL CHECK(N)
END
SUB CHECK (N)
    IF N >= 18 THEN
        PRINT "ELIGIBLE TO VOTE"
    ELSE
        PRINT "NOT ELIGIBLE TO VOTE"
    END IF
END SUB

30. Write a program to create sub procedure long (A$,B$,C$) to enter name of three students, then determine the longest name.
DECLARE SUB LONGEST (A$,B$,C$)
CLS
INPUT "ENTER THREE STUDENTS NAME"; A$, B$, C$
CALL LONGEST(A$, B$, C$)
END
SUB LONGEST (A$, B$, C$)
    FOR I = 2 TO 3
        INPUT "ENTER THREE STUDENTS NAME"; B$
        IF LEN(B$) > LEN(A$) THEN A$ = B$
    NEXT I
    PRINT "LONGEST NAME = "; A$
END SUB

DECLARE SUB LONGEST (N)
CLS
CALL LONGEST(N)
END
SUB LONGEST (N)
    DIM N(3) AS STRING
    FOR I = 1 TO 3
        INPUT "ENTER 3 STUDENTS NAME"; N(I)
    NEXT I
    A$ = N(1)
    FOR J = 2 TO 3
        IF LEN(N(J)) > LEN(A$) THEN A$ = N(J)
    NEXT J
    PRINT "THE LONGEST NAME = "; A$
END SUB

31. Write a program to create a sub procedure display(N) to display square and cube of numbers from 1 to Nth term.
DECLARE SUB SQUARE (N)
DECLARE SUB CUBE (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SQUARE(N)
CALL CUBE(N)
END
SUB SQUARE (N)
    FOR I = 1 TO N
        S = I ^ 2
        PRINT "SQUARE NUMBER = "; S
    NEXT I
END SUB
SUB CUBE (N)
    FOR I = 1 TO N
        C = I ^ 3
        PRINT "CUBE NUMBER = "; C
    NEXT I
END SUB

32. Write a program to factorial of given number.
DECLARE SUB FACTO (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL FACTO(N)
END
SUB FACTO (N)
    F = 1
    FOR I = 1 TO N
        F = F * I
    NEXT I
    PRINT "FACTORIAL NUMBER = "; F
END SUB

33. Write a program to calculate H.C.F of any two entered numbers.
DECLARE SUB HCF (A,B)
CLS
INPUT "ENTER ANY TWO NUMBERS"; A, B
CALL HCF(A, B)
END
SUB HCF (A, B)
    WHILE A MOD B <> 0
        T = A MOD B
        A = B
        B = T
    WEND
    PRINT "HCF = "; B
END SUB

34. Write a program to define a sub procedure to print sum of ASCII codes of each characters of user entered word by the user.
DECLARE SUB SUM (A$)
CLS
INPUT "Enter a character"; A$
CALL SUM(A$)
END
SUB SUM (A$)
    S = 0
    FOR I = 1 TO LEN(A$)
        B$ = MID$(A$, I, 1)
        A = ASC(B$)
        S = S + A
    NEXT I
    PRINT "SUM"; S
END SUB

By Using Select Case
DECLARE SUB SUM (A$)
CLS
INPUT "ENTER ANY WORD"; A$
CALL SUM(A$)
END
SUB SUM (A$)
    FOR I = 1 TO LEN(A$)
        B$ = MID$(A$, I, 1)
        N = ASC(B$)
        SELECT CASE N
            CASE 65 TO 90
            CASE 97 TO 122
                S = S + N
        END SELECT
    NEXT I
    PRINT "SUM"; S
END SUB

35. Write a program to create a sub procedure to count number of words in the given sentence.
DECLARE SUB COUNT (A$)
CLS
INPUT "ENTER ANY WORDS"; A$
CALL COUNT(A$)
END
SUB COUNT (A$)
    C = 0
    FOR I = 1 TO LEN(A$)
        B$ = MID$(A$, I, 1)
        C = C + 1
    NEXT I
    PRINT C
END SUB

36. Write a program to count number of odd digits and number of even digits in user entered six digits number.
DECLARE SUB COUNT (N)
CLS
CALL COUNT(N)
END
SUB COUNT (N)
    DIM N(6)
    FOR I = 1 TO 6
        INPUT "ENTER ANY 6 NUMBERS"; N(I)
    NEXT I
    FOR I = 2 TO 6
        IF N(I) MOD 2 = 0 THEN E = E + 1
        IF N(I) MOD 2 = 1 THEN O = O + 1
    NEXT I
    PRINT "EVEN NUMBER = "; E
    PRINT "ODD NUMBER = "; O
END SUB

37. Write programs using SUB…END SUB statement to generate the following output.

a.         POKHARA
   OKHAR
     KHA
       H

DECLARE SUB PATT (A$)
CLS
CALL PATT(A$)
END
SUB PATT (A$)
    A$ = "POKHARA"
    B = 1: C = 15
    FOR I = LEN(A$) TO 1 STEP -2
        PRINT TAB(C); MID$(A$, B, I)
        C = C + 1: B = B + 1
    NEXT I
END SUB

b.        L
AL
PAL
EPAL
NEPAL

DECLARE SUB PATT (A$)
CLS
CALL PATT(A$)
END
SUB PATT (A$)
    A$ = "NEPAL"
    FOR I = 1 TO 5
        PRINT RIGHT$(A$, I)
    NEXT I
END SUB

c.                  U
       PUT
      MPUTE
    OMPUTER
 COMPUTERS

DECLARE SUB PATT (A$)
CLS
CALL PATT(A$)
END
SUB PATT (A$)
    A$ = "COMPUTERS"
    C = 1: B = 35
    FOR I = 5 TO 1 STEP -1
        PRINT TAB(B); MID$(A$, I, C)
        C = C + 2
        B = B - 1
    NEXT I
END SUB

d.        COMPUTERS
   OMPUTER
     MPUTE
        PUT
           U
DECLARE SUB PATT (N)
CLS
CALL PATT(N)
END
SUB PATT (N)
    A$ = "COMPUTERS"
    T = 1
    FOR I = LEN(A$) TO 1 STEP -2
        PRINT TAB(T); MID$(A$, T, I)
        T = T + 1
    NEXT I
END SUB

e.        N
NE
NEP
NEPA
NEPAL

DECLARE SUB PATT (A$)
CLS
CALL PATT(A$)
END
SUB PATT (A$)
    A$ = "NEPAL"
    FOR I = 1 TO 5
        PRINT LEFT$(A$, I)
    NEXT I
END SUB

f.                 *
       ***
      *****
     ******

DECLARE SUB PATT (A$)
CLS
CALL PATT(A$)
END
SUB PATT (A$)
    A$ = "*******"
    T = 10
    FOR I = 1 TO 7 STEP 2
        PRINT TAB(T); LEFT$(A$, I)
        T = T - 1
    NEXT I
END SUB

38. Write programs to declare a sub-procedure module to generate the following numeric series.

a. 1, 11, 111, 1111, 11111
DECLARE SUB SERIES (N)
CLS
CALL SERIES(N)
END
SUB SERIES (N)
    N = 1
    FOR I = 1 TO 5
        PRINT N;
        N = N * 10 + 1
    NEXT I
END SUB

b. 1, 4, 9, 16, 25, …..upto 10th term
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 1 TO 10
        PRINT I ^ 2;
    NEXT I
END SUB

c. 3, 12, 27,…… upto 10th term
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 1 TO 10
        PRINT (I ^ 2) * 3;
    NEXT I
END SUB

d. 1, 12, 123, 1234, 12345
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 1 TO 5
        FOR J = 1 TO I
            PRINT J;
        NEXT J
        PRINT
    NEXT I
END SUB

e. 19, 17, 15, 13…..upto 1
DECLARE SUB SEREIS ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 19 TO 1 STEP -2
        PRINT I;
    NEXT I
END SUB

f. 33333, 3333, 333, 33, 3
DECLARE SUB SERIES (N)
CLS
CALL SERIES(N)
END
SUB SERIES (N)
    N = 33333
    FOR I = 1 TO 5
        PRINT N;
        N = N \ 10
        C = C + 1
    NEXT I
END SUB

g. 1, 2, 4, 7, 11…..upto 10th term
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    A = 1
    B = 1
    FOR I = 1 TO 10
        PRINT A;
        A = A + B
        B = B + 1
    NEXT I
END SUB

h. 2, 1, 3, 4, 7, 11….upto 11th term
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    A = 2
    B = 1
    PRINT A; B;
    FOR I = 1 TO 11
        C = A + B
        PRINT C;
        A = B
        B = C
    NEXT I
END SUB

i. 1, 121, 12321, 1234321, 12345321
DECLARE SUB SERIES ()
CLS
CALL SER
END
SUB SER ()
    N# = 1
    FOR I = 1 TO 5
        PRINT N# * N#;
        N# = N# * 10 + 1
    NEXT I
    END
END SUB

j. 1, 12, 123, 1234, 12345
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 1 TO 5
        FOR J = 1 TO I
            PRINT J;
        NEXT J
        PRINT
    NEXT I
END SUB

k. 55555, 4444, 333, 22, 1
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 5 TO 1 STEP -1
        FOR J = 1 TO I
            PRINT I;
        NEXT J
        PRINT
    NEXT I
END SUB

l. 1, 10, 101, 1010, 10101
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES ()
    FOR I = 1 TO 5
        FOR J = 1 TO I
            PRINT J MOD 2;
        NEXT J
        PRINT
    NEXT I
END SUB

FUNCTION Procedure

Write QBASIC program using FUNCTION Procedure (FUNCTION…..END FUNCTION) statement.

1. Write a program to convert Nepali Currency (NC) to equivalent American dollar where one dollar = 95(NC)
DECLARE FUNCTION CONVERT (D)
CLS
INPUT "ENTER CURRENCY VALUE IN DOLLAR"; D
PRINT "NEPALI CURRENCY VALUE = "; CONVERT(D)
END
FUNCTION CONVERT (D)
    NC = D * 95
    CONVERT = NC
END FUNCTION

2. Write a program to declare a user-defined function to reverse a given string. The program should input string in the main module and reverse the same using user defined function.
DECLARE FUNCTIOIN REV$ (A$)
CLS
INPUT "ENTER ANY STRING"; A$
PRINT "THE REVERSE STRING IS = "; REV$(A$)
R$ = REV$(A$)
END
FUNCTION REV$ (A$)
    FOR I = LEN(A$) TO 1 STEP -1
        R$ = R$ + MID$(A$, I, 1)
    NEXT I
    REV$ = R$
END FUNCTION

3. Write a program to calculate the simple interest for a given principal, time and rate of interest.
DECLARE FUNCTION INTEREST (P,T,R)
CLS
INPUT "ENTER THE PRINCIPAL"; P
INPUT "ENTER THE TIME"; T
INPUT "ENTER THE RATE"; R
PRINT "SIMPLE INTEREST ="; INTEREST(P, T, R)
END
FUNCTION INTEREST (P, T, R)
    SI = P * T * R / 100
    INTEREST = SI
END FUNCTION

4. Write a program to create a function area (L,B,H) to calculate and print Total Surface Area (TSA) of box. [TSA=2(LB+BH+HL)]
DECLARE FUNCTION AREA (L,B,H)
CLS
INPUT "ENTER THE LENGTH OF BOX"; L
INPUT "ENTER THE BREADTH OF BOX"; B
INPUT "ENTER THE HEIGHT OF BOX"; H
PRINT "TOTAL SURFACE AREA OF BOX"; AREA(L, B, H)
END
FUNCTION AREA (L, B, H)
    TSA = 2 * (L * B + B * H + H * L)
    AREA = TSA
END FUNCTION

5. Write a program to convert the entered octal number to equivalent decimal number.
DECLARE FUNCTION CONVERT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
WHILE N <> 0
    R = N MOD 10
    D = D + R * 8 ^ P
    P = P + 1
    N = N \ 10
WEND
PRINT "THE EQUIVALENT DECIMAL NUMBER IS = "; D
END
FUNCTION CONVERT (N)
    CONVERT = D
END FUNCTION

6. Write a program to print the smallest number among three numbers.
DECLARE FUNCTION SMALLEST (A,B,C)
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
PRINT "THE SMALLEST NUMBER"; SMALLEST(A, B, C)
END
FUNCTION SMALLEST (A, B, C)
    IF A < B AND A < C THEN
        SMALLEST = A
    ELSEIF B < A AND B < C THEN
        SMALLEST = B
    ELSE
        SMALLEST = C
    END IF
END FUNCTION

7. Write a program to declare a user defined function to count total number of vowels in a given string.
DECLARE FUNCTION VOWEL (A$)
CLS
INPUT "ENTER ANY STRING WORDS"; A$
PRINT "TOTAL NUMBER OF VOWELS"; VOWEL(A$)
END
FUNCTION VOWEL (A$)
    FOR I = 1 TO LEN(A$)
        B$ = MID$(A$, I, 1)
        IF B$ = "A" OR B$ = "E" OR B$ = "I" OR B$ = "O" OR B$ = "U" THEN
            C = C + 1
        END IF
        VOWEL = C
    NEXT I
END FUNCTION

8. Write a program to declare a user defined function to return the total number of spaces in a given sentence.
DECLARE FUNCTION SPAS(N$)
CLS
INPUT " ENTER YOUR SENTENCE"; C$
PRINT " TOTAL SPACES IN YOUR SENTENCE="; SPAS(C$)
END

FUNCTION SPAS (N$)
    FOR I = LEN(N$) TO 1 STEP -1
        D$ = MID$(N$, I, 1)
        IF D$ = " " THEN COUNT = COUNT + 1
    NEXT I
    SPAS = COUNT
END FUNCTION

9. Using FUNCTION….END FUNCTION, write a program to calculate the area of circle. [PIR2]
DECLARE FUNCTION RADIUS (R)
CLS
INPUT "ENTER RADIUS OF CIRCLE"; R
PRINT "THE AREA OF CIRCLE"; RADIUS(R)
END
FUNCTION RADIUS (R)
    A = 3.14 * R ^ 2
    RADIUS = A
END FUNCTION

10. Write a function rev (N) that returns a number in reverse order. [E.g: 123 to 321]
DECLARE FUNCTION REV (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "THE REVERSE NUMBER IS"; REV(N)
END
FUNCTION REV (N)
    WHILE N <> 0
        R = N MOD 10
        S = S * 10 + R
        N = N \ 10
    WEND
    REV = S
END FUNCTION

11. Write a program which asks three numbers and displays the middle number among them.
DECLARE FUNNCTION MIDDLE (A,B,C)
CLS
INPUT "ENTER ANY THREE NUMBERS"; A, B, C
PRINT "THE MIDDLE NUMBERS IS"; MIDDLE(A, B, C)
END
FUNCTION MIDDLE (A, B, C)
    IF A > B AND A < C OR A < B AND A > C THEN
        MIDDLE = A
    ELSEIF B > A AND B < C OR B < A AND B > C THEN
        MIDDLE = B
    ELSE
        MIDDLE = C
    END IF
END FUNCTION

12. Write a program to check whether supplied word is palindrome or not.
DECLARE FUNCTION PALIN$ (A$)
CLS
INPUT "ENTER ANY WORD"; A$
FOR I = LEN(A$) TO 1 STEP -1
    R$ = R$ + MID$(A$, I, 1)
NEXT I
IF R$ = A$ THEN
    PRINT "PALINDROME"
ELSE
    PRINT "NOT PALINDROME"
END IF
END
FUNCTION PALIN$ (A$)
    PALIN$ = R$
END FUNCTION

13. Write a program to calculate and print area of a square.
DECLARE FUNCTION AREA (L)
CLS
INPUT "ENTER THE LENGTH OF SQUARE"; L
PRINT "AREA OF A SQUARE"; AREA(L)
END
FUNCTION AREA (L)
    A = L ^ 2
    AREA = A
END FUNCTION

14. Write a program to convert temperature centigrade in Fahrenheit. [F = 9/5C+32]
DECLARE FUNCTION CONVERT (C)
CLS
INPUT "ENTER TEMPERATURE IN CELCIUS"; C
PRINT "TEMPERATURE IN FAHRENHEIT"; CONVERT(C)
END
FUNCTION CONVERT (C)
    F = 9 * C / 5 + 32
    CONVERT = F
END FUNCTION

15. Write a program to detect and print whether an entered number is odd or even.
DECLARE FUNCTION CHECK$(N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "THE GIVEN NUMBER IS = "; CHECK$(N)
END
FUNCTION CHECK$ (N)
    IF N MOD 2 = 0 THEN
        CHECK$ = "EVEN NUMBER"
    ELSE
        CHECK$ = "ODD NUMBER"
    END IF
END FUNCTION

16. Write a program to check and print whether an entered number is prime or composite.
DECLARE FUNCTION CHECK$(N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "THE NUMBER IS = "; CHECK$(N)
END
FUNCTION CHECK$ (N)
    FOR I = 1 TO N
        IF N MOD I = 0 THEN C = C + 1
    NEXT I
    IF C = 2 THEN
        CHECK$ = "EVEN NUMBER"
    ELSE
        CHECK$ = "ODD NUMBER"
    END IF
END FUNCTION

17. Write a program to find the longest string among three strings.
DECLARE FUNCTION LONGEST$(A$,B$,C$)
CLS
INPUT "ENTER FIRST STRING"; A$
INPUT "ENTER SECOND STRING"; B$
INPUT "ENTER THIRD STRING"; C$
PRINT "THE LONGEST STRING IS = "; LONGEST$(A$, B$, C$)
END
FUNCTION LONGEST$ (A$, B$, C$)
    IF LEN(A$) > LEN(B$) AND LEN(A$) > LEN(C$) THEN
        L$ = A$
    ELSEIF LEN(B$) > LEN(A$) AND LEN(B$) > LEN(C$) THEN
        L$ = B$
    ELSE
        L$ = C$
    END IF
    LONGEST$ = L$
END FUNCTION

18. Write a program that allows us to enter a number then check whether the given number is perfect square or not.
DECLARE FUNCTION PERFECT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
S = SQR(N)
IF S = INT(S) THEN
    PRINT "PERFECT SQUARE"
ELSE
    PRINT "NOT PERFECT SQUARE"
END IF
END
FUNCTION PERFECT (N)
    PERFECT = S
END FUNCTION

19. Write a program to create a function square (N) and use this function to calculate sum of square of numbers from 1 to that number.
DECLARE FUNCTION CAL (N)
CLS
INPUT "ENTER ANY NUMBER"; N
FOR I = 1 TO N
    PRINT I ^ 2;
    S = S + I ^ 2
NEXT I
PRINT "SUM = "; S
END
FUNCTION CAL (N)
    CAL = S
END FUNCTION

20. Write a program to create a function procedure check$(A,B,C) to enter length of three sides then check whether a triangle can be formed or not.
DECLARE FUNCTION CHECK(A,B,C)
CLS
INPUT "ENTER THREE SIDES OF A TRIANGLE"; A, B, C
C = CHECK(A, B, C)
IF (A + B) > C AND (B + C) > A AND (A + C) > B THEN
    PRINT "THE TRIANGLE CAN BE FORMED"
ELSE
    PRINT "THE TRIANGLE CAN NOT BE FORMED"
END IF
END
FUNCTION CHECK (A, B, C)
    CHECK = C
END FUNCTION

21. Write a program to count the occurrence (repetition) of any entered character in the given string.
DECLARE FUNCTION COUNT(C)
CLS
INPUT "ENTER A WORD"; W$
INPUT "ENTER WHICH CHARACTER IS TO BE COUNT "; A$
FOR I = 1 TO LEN(W$)
    R$ = MID$(W$, I, 1)
    IF R$ = A$ THEN C = C + 1
NEXT I
PRINT C
END
FUNCTION COUNT (C)
    COUNT = C
END FUNCTION

22. Write a program using FUNCTION…END FUNCTION to count the number of words in a sentence.
DECLARE FUNCTION COUNT (S$)
CLS
INPUT "ENTER ANY STRING"; S$
PRINT "TOTAL NO. OF WORDS= "; COUNT(S$)
END

FUNCTION COUNT (S$)
    WC = 1
    FOR I = 1 TO LEN(S$)
        B$ = MID$(S$, I, 1)
        IF B$ = " " THEN WC = WC + 1
    NEXT I
    COUNT = WC
END FUNCTION

23. Write a program to calculate total distance (S) covered by an object where initial velocity (u), acceleration (a) and time (t) is input from the keyboard by the user while executing the program. [s=ut+1/2at2]
DECLARE FUNCTION DIST (U,A,T)
CLS
INPUT "ENTER THE INITIAL VELOCITY"; U
INPUT "ENTER THE ACCELERATION"; A
INPUT "ENTER THE TIME"; T
PRINT "TOTAL DISTANCE IS = "; DIST(U, A, T)
END
FUNCTION DIST (U, A, T)
    S = U * T + 1 / 2 * A * T ^ 2
    DIST = S
END FUNCTION

24. Write a program to input complete date of birth of a person then determine his/her present age.
DECLARE FUNCTION DIS$(D$)
CLS
INPUT "ENTER YOU DATE OF BIRTH"; D$
P = VAL(RIGHT$(D$, 4))
Q = VAL(RIGHT$(DATE$, 4))
R = Q - P
PRINT "YOUR AGE IS = "; R
END
FUNCTION DIS$ (D$)
    DIS$ = D$
END FUNCTION

25. Write a program to define a function procedure area (R) to calculate and display area of sphere. [Area = 4/2pir3]
DECLARE FUNCTION CAL (R)
CLS
INPUT "ENTER THE RADIUS OF SPHERE"; R
PRINT "TOTAL AREA OF SPHERE = "; CAL(R)
END
FUNCTION CAL (R)
    AREA = 4 / 2 * 3.14 * R ^ 3
    CAL = AREA
END FUNCTION

26. Write a program to create two different functions for calculating and printing sum, difference and product of two entered numbers.
DECLARE FUNCTION SUM (A,B)
DECLARE FUNCTION DIFFERENCE (A,B)
DECLARE FUNCTION PRODUCT (A,B)
CLS
INPUT "ENTER ANY TWO NUMBERS"; A, B
PRINT "SUM OF TWO NUMBERS "; SUM(A, B)
PRINT "DIFFERENCE OF TWO NUMBERS "; DIFFERENCE(A, B)
PRINT "PRODCUT OF TWO NUMBERS "; PRODUCT(A, B)
END
FUNCTION SUM (A, B)
    S = A + B
    SUM = S
END FUNCTION
FUNCTION DIFFERENCE (A, B)
    D = A - B
    DIFFERENCE = D
END FUNCTION
FUNCTION PRODUCT (A, B)
    P = A * B
    PRODUCT = P
END FUNCTION

27. Write a program to calculate area of triangle where the values of three sides are given.
DECLARE FUNCTION AREA (A,B,C)
CLS
INPUT "ENTER VALUE OF FIRST SIDE"; A
INPUT "ENTER VALUE OF SECOND SIDE"; B
INPUT "ENTER VALUE OF THIRD SIDE"; C
PRINT "AREA OF TRIANGLE"; AREA(A, B, C)
END
FUNCTION AREA (A, B, C)
    S = (A + B + C) / 2
    A = (S * (S - A) * (S - B) * (S - C)) ^ (1 / 2)
    AREA = A
END FUNCTION

28. Write a program to check whether the entered number is exactly divisible by 13 or not.
DECLARE FUNCTION CHECK$ (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT CHECK$(N)
END
FUNCTION CHECK$ (N)
    IF N MOD 13 = 0 THEN
        CHECK$ = "DIVISIBLE BY 13"
    ELSE
        PRINT "NOT DIVISIBLE BY 13"
    END IF
END FUNCTION

29. Write a program to define an user's function reverse (N) to display an entered number in reverse order.
DECLARE FUNCTION REV (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "THE REVERSE NUMBER IS = "; REV(N)
END
FUNCTION REV (N)
    WHILE N <> 0
        R = N MOD 10
        S = S * 10 + R
        N = N \ 10
    WEND
    REV = S
END FUNCTION

30. Write a program to calculate LCM of any two entered numbers.
DECLARE FUNCTION LCM (A,B)
CLS
INPUT "ENTER ANY TWO NUMBER"; A, B
PRINT "LCM OF TWO NUMBER"; LCM(A, B)
END
FUNCTION LCM (A, B)
    M = A
    N = B
    WHILE A <> 0
        R = B MOD A
        B = A
        A = R
    WEND
    L = (M * B) / B
    LCM = L
END FUNCTION

31. Write a program to create a function procedure rev$(W$,R$) to display the entered string in reverse order.
DECLARE FUNCTION REV$(W$)
CLS
INPUT "ENTER ANY STRING WORD"; W$
PRINT "THE REVERSE WORD IS = "; REV$(W$)
END
FUNCTION REV$ (W$)
    FOR I = LEN(W$) TO 1 STEP -1
        R$ = R$ + MID$(W$, I, 1)
    NEXT I
    REV$ = R$
END FUNCTION

32. Write a program to convert temperature Fahrenheit to equivalent centigrade.
DECLARE FUNCTION CONVERT (F)
CLS
INPUT "ENTER TEMPERATURE IN FAHRENHEIT"; F
PRINT "TEMPERATURE IN CENITGRADE"; CONVERT(F)
END
FUNCTION CONVERT (F)
    C = (F - 32) * (5 / 9)
    CONVERT = C
END FUNCTION

33. Write a program to check whether entered number is palindrome or not.
DECLARE FUNCTION PALIN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
P = PALIN(N)
END
FUNCTION PALIN (N)
    A = N
    S = 0
    WHILE N <> 0
        R = N MOD 10
        S = S * 10 + R
        N = N \ 10
    WEND
    IF A = S THEN
        PRINT "PALINDROME"
    ELSE
        PRINT "NOT PALINDROME"
    END IF
    PALIN = P
END FUNCTION

34. Write a program to create a function product (N) to calculate and print the product of each digit of user entered number.
DECLARE FUNCTION PRODUCT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "THE PRDUCT NUMBER IS ="; PRODUCT(N)
END
FUNCTION PRODUCT (N)
    P = 1
    WHILE N <> 0
        R = N MOD 10
        P = P * R
        N = N \ 10
    WEND
    PRODUCT = P
END FUNCTION

Write programs in QBASIC to display the following numeric series using FUNCTION….END FUNCTION.

a. 1, 8, 27…..up to 10th term
DECLARE FUNCTION SERI (N)
CLS
FOR I = 1 TO 10
    PRINT I ^ 3;
NEXT I
END
FUNCTION SERI (N)
    SERI = I
END FUNCTION

b. 2, 1, 3, 4, 7, 11….. upto 11th term
DECLARE FUNCTION SERI (N)
CLS
A = 2
B = 1
PRINT A; B;
FOR I = 1 TO 11
    C = A + B
    PRINT C;
    A = B
    B = C
NEXT I
END
FUNCTION SERI (N)
    SERI = I
END FUNCTION

c. 50, 45, 35,….. 5
DECLARE FUNCTION SERI ()
CLS
FOR I = 50 TO 5 STEP -5
    PRINT I;
NEXT I
END
FUNCTION SERI ()
    SERI = I
END FUNCTION

d. 1, 22, 333, 4444, 55555
DECLARE FUNCTION SER ()
CLS
FOR I = 1 TO 5
    FOR J = 1 TO I
        PRINT I;
    NEXT J
    PRINT
NEXT I
END
FUNCTION SER ()
    SER = I
END FUNCTION

e. 97531, 9753, 975, 97, 9
DECLARE FUNCTION SER ()
CLS
FOR I = 1 TO 9 STEP 2
    FOR J = 9 TO I STEP -2
        PRINT J;
    NEXT J
    PRINT
NEXT I
END
FUNCTION SER ()
    SER = J
END FUNCTION

Write a program to display the following string pattern using FUNCTION……END FUNCTION statement.

a.         SCIENCE
  CIENC
     IEN
       E
DECLARE FUNCTION SER ()
CLS
A$ = "SCIENCE"
C = 1: B = 35
FOR I = LEN(A$) TO 1 STEP -2
    PRINT TAB(B); MID$(A$, C, I)
    C = C + 1: B = B + 1
NEXT I
END
FUNCTION SER ()
    SER = I
END FUNCTION

b.        C
COM
COMPU
COMPUTE
COMPUTERS
DECLARE FUNCTION SER ()
CLS
A$ = "COMPUTERS"
FOR I = 1 TO LEN(A$) STEP 2
    PRINT LEFT$(A$, I)
NEXT I
END
FUNCTION SER ()
    SER = I
END FUNCTION




Post a Comment

0 Comments