# Modular Programming in QBASIC Examples 1. Test whether the given number is positive or negative.
SUB PROCEDURE
DECLARE SUBTEST(N)
CLS
INPUT "Enter a number"; N
CALL TEST(N)
END
SUB TEST(N)
IF N>0 THEN
PRINT N; "is positive number"
ELSE
PRINT N; "is negative number"
END IF
END SUB

FUNCTION PROCEDURE
DECLARE FUNCTION CHECK\$ (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT N; "IS "; CHECK\$(N)
END
FUNCTION CHECK\$ (N)
IF N > 0 THEN
CHECK\$ = "POSITIVE NUMBER"
ELSEIF N < 0 THEN CHECK\$ = "NEGATIVE NUMBER" ELSE CHECK\$ = "ZERO" END IF END FUNCTION

2. Accept any three different numbers and find the maximum number among them.
Ans:
DECLARE SUB MAX(A,B,C)
CLS
INPUT "Enter any three number"; A,B,C
CALL MAX(A,B,C)
END
SUB MAX(A,B,C)
IF A>B AND A>C THEN
PRINT A; "is maximum number"
ELSEIF B>A AND B>C THEN
PRINT B; "is maximum number"
ELSE
PRINT C; "is maximum number"
END IF
END SUB

3. Declare a SUB procedure module to generate multiplication table of any non-negative number, where number is passed as a parameter.
SUB Procedure
DECLARE SUB MUL(N)
CLS
INPUT "Enter a number"; N
CALL MUL(N)
END
SUB MUL(N)
FOR I = 1 TO 10
PRINT N; "*";I; "="; N*I
NEXT I
END SUB

4. Reversing Word
SUB Procedure

DECLARE SUB REV(N\$)
CLS
INPUT "ENTER A WORD"; W\$
CALL REV(W\$)
END
SUB REV (N\$)
FOR I = LEN(N\$) TO 1 STEP -1
A\$ = A\$ + MID\$(N\$, I, 1)
NEXT I
PRINT "REVERSED STRING="; A\$
END SUB

FUNCTION Procedure
CLS
INPUT "ENTER A STRING"; W\$
PRINT "REVERSED STRING="; REV\$(W\$)
END
FUNCTION REV\$ (N\$)
FOR I = LEN(N\$) TO 1 STEP -1
A\$ = A\$ + MID\$(N\$, I, 1)
NEXT I
REV\$ = A\$
END FUNCTION

5. Prime or Composite
FUNCTION Procedure
DECLARE FUNCTION PRIME(N)
CLS
INPUT "ENTER ANY NUMBER"; N
P = PRIME(N)
IF P = 2 THEN
PRINT N; "IS PRIME NUMBER"
ELSE
PRINT N; "IS COMPOSITE NUMBER"
END IF
END
FUNCTION PRIME (N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
PRIME = C
END FUNCTION

6. ARMSTRONG or not
[An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Eg: 153 = 1^3 + 5^3 + 3^3= 153]
SUB Procedure
DECLARE SUB ARMS (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL ARMS(N)
END
SUB ARMS (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 A; "IS ARMSTRONG NUMBER"
ELSE
PRINT A; "IS NOT ARMSTRONG NUMBER"
END IF
END SUB

FUNCTION Procedure
DECLARE FUNCTION ARMS (N)
CLS
INPUT "ENTER ANY NUMBER"; N
A = N
AR = ARMS(N)
IF A = AR THEN
PRINT A; "IS ARMSTRONG NUMBER"
ELSE
PRINT A; "IS NOT ARMSTRONG NUMBER"
END IF
END
FUNCTION ARMS (N)
S = 0
WHILE N <> 0
R = N MOD 10
S = S + R ^ 3
N = N \ 10
WEND
ARMS = S
END FUNCTION

7. Input String and Count Total Number of vowels
SUB Procedure
DECLARE SUB COUNT (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
CALL COUNT(S\$)
END
SUB COUNT (S\$)
VC = 0
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ = "A" OR C\$ = "E" OR C\$ = "I" OR C\$ = "O" OR C\$ = "U" THEN
VC = VC + 1
END IF
NEXT I
PRINT "TOTAL NO. OF VOWELS= "; VC
END SUB

FUNCTION PROCEDURE
DECLARE FUNCTION COUNT (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
PRINT "TOTAL NO. OF VOWELS= "; COUNT(S\$)
END
FUNCTION COUNT (S\$)
VC = 0
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ = "A" OR C\$ = "E" OR C\$ = "I" OR C\$ = "O" OR C\$ = "U" THEN
VC = VC + 1
END IF
NEXT I
COUNT = VC
END FUNCTION

8. Input String and display only vowels
SUB PROCEDURE
DECLARE SUB DISPV (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
CALL DISPV(S\$)
END

SUB DISPV(S\$)
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ = "A" OR C\$ = "E" OR C\$ = "I" OR C\$ = "O" OR C\$ = "U" THEN
PRINT B\$
END IF
NEXT I
END SUB

FUNCTION PROCEDURE
DECLARE FUNCTION DISP (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
VC = DISP(S\$)
END
FUNCTION DISP (S\$)
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ = "A" OR C\$ = "E" OR C\$ = "I" OR C\$ = "O" OR C\$ = "U" THEN
PRINT B\$
END IF
NEXT I
END FUNCTION

9.Input String and display only consonant
SUB Procedure
DECLARE SUB DISC (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
CALL DISC(S\$)
END
SUB DISC (S\$)
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ <> "A" AND C\$ <> "E" AND C\$ <> "I" AND C\$ <> "O" AND C\$ <> "U" AND C\$ <> " " AND C\$ <> "." THEN
PRINT B\$
END IF
NEXT I
END SUB

FUNCTION Procedure
DECLARE FUNCTION DISPC (S\$)
CLS
INPUT "ENTER ANY STRING"; S\$
CC = DISPC(S\$)
END
FUNCTION DISPC (S\$)
FOR I = 1 TO LEN(S\$)
B\$ = MID\$(S\$, I, 1)
C\$ = UCASE\$(B\$)
IF C\$ <> "A" AND C\$ <> "E" AND C\$ <> "I" AND C\$ <> "O" AND C\$ <> "U" AND C\$ <> " " AND C\$ <> "." THEN
PRINT B\$
END IF
NEXT I
END FUNCTION

10.Sum of EVEN Number
SUB Procedure
DECLARE SUB SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMEVEN(N)
END
SUB SUMEVEN (N)
S = 0
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R
N = N \ 10
WEND
PRINT "SUM OF EVEN DIGITS"; S
END SUB

FUNCTION Procedure
DECLARE FUNCTION SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMEVEN(N)
PRINT "SUM OF EVEN DIGITS"; SU
END
FUNCTION SUMEVEN (N)
S = 0
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R
N = N \ 10
WEND
SUMEVEN = S
END FUNCTION

11. Centigrade to Fahrenheit
SUB Procedure
DECLARE SUB CONVERT (C)
CLS
INPUT "ENTER TEMPERATURE IN CELCIUS"; C
CALL CONVERT(C)
END
SUB CONVERT (C)
F = C * (9 / 5) + 160
PRINT "TEMPERATURE IN FARENHEIT="; F
END SUB

FUNCTION Procedure
DECLARE FUNCTION CONVERT (C)
CLS
INPUT "ENTER TEMPERATURE IN CELSIUS"; C
PRINT "TEMPERATURE IN FAHRENHEIT="; CONVERT(C)
END
FUNCTION CONVERT (C)
F = C * (9 / 5) + 160
CONVERT = F
END FUNCTION

12. Perimeter or rectangle 2(L+B)
SUB Procedure
DECLARE SUB PERIMETER (L, B)
CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
CALL PERIMETER(L, B)
END
SUB PERIMETER (L, B)
P = 2 * (L + B)
PRINT "PERIMETER OF RECTANGLE "; P
END SUB

FUNCTION Procedure
DECLARE FUNCTION PERIMETER (L, B)
CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
PR = PERIMETER(L, B)
PRINT "PERIMETER OF RECTANGLE "; PR
END
FUNCTION PERIMETER (L, B)
P = 2 * (L + B)
PERIMETER = P
END FUNCTION

13. Convert Nepalese Currency to Indian Currency
SUB Procedure
DECLARE SUB CONV(N)
CLS
INPUT "ENTER NEPALESE CURRENCY "; P
CALL CONV(P)
END
SUB CONV (P)
C = P / 1.6
PRINT "INDIAN CURRENCY "; C
END SUB

FUNCTION Procedure
DECLARE FUNCTION CONV(N)
CLS
INPUT "ENTER NEPALESE CURRENCY "; P
PRINT "INDIAN CURRRENCY = "; CONV(P)
END
FUNCTION CONV (P)
CONV = P / 1.6
END FUNCTION

14. Square of all digit of input number
SUB Procedure
DECLARE SUB SQU(A)
CLS
INPUT "ENTER A NUMBER"; N
CALL SQU(N)
END
SUB SQU (N)
WHILE N <> 0
R = N MOD 10
S = R ^ 2
PRINT S
N = N \ 10
WEND
END SUB

15. Greatest among two number
FUNCTION PROCEDURE
DECLARE FUNCTION MAX(A,B)
CLS
INPUT "Enter any two number"; A, B
PRINT "The greater number is"; MAX(A, B)
END
FUNCTION MAX (A, B)
IF A > B THEN
MAX = A
ELSE
MAX = B
END IF
END FUNCTION

16.Using user defined function, write a program to input monthly income in parameter then computer annual tax to be paid. The tax rate is 15% if annual income is above Rs. 200000, otherwise tax rate is 1%.
FUNCTION PROCEDURE
DECLARE FUNCTION TAX(I)
CLS
INPUT "Enter monthly income"; I
PRINT "Tax to be paid="; TAX(I)
END
FUNCTION TAX (I)
A = I * 12
IF A > 200000 THEN
TAX = 15 / 100 * A
ELSE
TAX = 1 / 100 * A
END IF
END FUNCTION #### About Prajwal Rai

View all posts by Prajwal Rai →