SUB & FUNCTION Procedure - QBasic

QBasic Programs – Using SUB and FUNCTION Procedure

QBasic, as its name suggests (Quick Beginners All-Purpose Symbolic Instruction Code) is a basic programming language that helps you build a basic concept for programming. It helps you understand Looping, Conditional statements, and many more. If you are good at QBasic, it will definitely help you a lot in the future when you try learning JS and even C.

Content Highlights
What QBasic Looks Like

You might have grabbed a quick knowledge that QBasic is not a professional programming language. It is a simple and basic programming language for you to start learning to program. Though it does not help you build professional programs, you can definitely build basic programs and games. Program writing is not difficult when you know the Syntax and the logic behind problem-solving.

You might be interested in: How to run QBasic on Android Phones

Logic plays a huge role in program writing. You must have known by till know what Qbasic is but for a quick intro. QBasic is a Modular Programming Language because it allows users to work on different Blocks or say module, designed to help beginner programmers to develop the basic concepts for getting started with programming.

An example of QBasic Programs Output

Here we have given you solutions for some SUB QBasic Programs as well as FUNCTION Procedure programs. Programs that you will come across here today will help a student of any grade and also for SEE exams. We hope it helps you clear any of your doubts.

Lets gets started with the QBasic Programs!

1. Write a program to check whether the input number is positive, negative, or zero.

Using SUB Procedure:

DECLARE SUB CHECK(N)
CLS
INPUT “Enter any number”; N
CALL CHECK(N)
END
SUB CHECK(N)
IF N > 0 THEN
PRINT N; “It is a positive number”
ELSEIF N < 0 THEN
PRINT N; “It is a negative number”
ELSE
PRINT N; “The number is Zero”
END IF
END SUB

Using 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$ = “It is a positive number”
ELSEIF N < 0 THEN
CHECK$ = “It is a negative number”
ELSE
CHECK$ = “The number is Zero”
END IF
END FUNCTION


2. Write a program to test whether the given number is positive or negative using SUB ………. END SUB.

Program Using SUB Procedure:

DECLARE NUMTEST(N)
CLS
INPUT “Enter a number”; N
CALL TEST(N)
END
SUB TEST(N)
IF N > 0 THEN
PRINT “The given number” N; “is a positive number”
ELSE
PRINT “The given number” N; “is a negative number”
END IF
END SUB


3. Write a program which accepts any three different numbers and find the maximum number among them using the CALL statement.

Program Using SUB Procedure:

DECLARE SUB MAX(A, B, C)
CLS
INPUT “Enter the First number”; A
INPUT “Enter the Second number”; B
INPUT “Enter the Third number”; 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 the maximum number”
ELSE
PRINT C; “is the maximum number”
END IF
END SUB


4. Write a program to display greater number among any two numbers using FUNCTION ………. END FUNCTION.

Program Using FUNCTION Procedure:

DECLARE FUNCTION GRE(A, B)
CLS
INPUT “Enter the First number”; A
INPUT “Enter the Second number”; B
PRINT “The Greater Number is”; GRE(A, B)
END
FUNCTION GRE(A, B)
IF A > B THEN
GRE = A
ELSE
GRE = B
END IF
END FUNCTION


5. Write a program to check whether a supplied number is perfect square or not.

Using SUB Procedure:

DECLARE SUB PSQR(N)
CLS
INPUT “Enter a Number”;N
CALL PSQR(N)
END
SUB PSQR(N)
S = SQR(N)
IF S = INT(S) THEN
PRINT “The supplied number is a perfect square”
ELSE
PRINT “The supplied number is not a perfect square”
END IF
END SUB

Using FUNCTION Procedure:

DECLARE FUNCTION PSQR$(N)
CLS
INPUT “Enter a Number”; N
PRINT PSQR$(N)
END
FUNCTION PSQR$(N)
S = SQR(N)
IF S = INT(S) THEN
PSQR$ = “The supplied number is a perfect square”
ELSE
PSQR$ = “The supplied number is not a perfect square”
ENDIF
END SUB


6. Write a program to calculate the volume of a cylinder using FUNCTION ………. END FUNCTION. [Hint: PI * R ^ 2 * H]

Program Using FUNCTION Procedure:

DECLARE FUNCTION VOL(R, H)
CLS
CONST PI = 3.14
INPUT “Enter Radius of the Cylinder”; R
INPUT “Enter Height of the Cylinder”; H
PRINT “The volume of the cylinder is”; VOL(R, H)
END
FUNCTION VOL(R, H)
V = PI * R ^ 2 * H
VOL = V
END FUNCTION


7. Write a program to find the factorial number of any non-negative number using FUNCTION ………. END FUNCTION.

Program Using FUNCTION Procedure:

DECLARE FUNCTION FACT(N)
CLS
INPUT “Enter a non-negative number”; N
PRINT “The factorial of given number is “; FACT(N)
END
FUNCTION FACT(N)
F = 1
FOR I = 1 TO N
F = F * I
NEXT I
FACT = F
END FUNCTION


8. Write a program to display the reverse of the entered number.

Using SUB Procedure:

DECLARE SUB REV(N)
CLS
INPUT “Enter a number”; N
CALL REV(N)
END
SUB REV(N)
S=0
WHILE N <> 0
R = N MOD 10
S = S * 10 + R
N = N \ 10
WEND
PRINT “The reversed form is “; S
END SUB

Using FUNCTION Procedure:

DECLARE FUNCTION REV(N)
CLS
INPUT “Enter a number”; N
PRINT “The reversed form is”; REV(N)
END
FUNCTION REV(N)
S = 0
WHILE N <> 0
R = N MOD 10
S = S * 10 + R
N = N \ 10
WEND
REV = S
END FUNCTION


9. Write a program to declare a SUB procedure module to generate multiplication table of any non-negative number, where number is passed as a parameter by using SUB ………. END SUB Statement. (Upto 10)

Program Using SUB Procedure:

DECLARE SUB MULT(N)
CLS
INPUT “Enter a number”; N
CALL MULT(N)
END
SUB MULT(N)
FOR I = 1 TO 10
PRINT N; “*”; I; “=”; N*I
NEXT I
END SUB


Q no. 10. Write a program that asks any number and calculates its factors using a SUB procedure.

Program Using SUB Procedure :

DECLARE SUB FACT(N)
CLS
INPUT “Enter a number”; N
CALL FACT(N)
END
SUB FACT(N)
PRINT “The factors of”; N; “are”
FOR I = 1 TO N
IF N MOD I = 0 THEN PRINT I
NEXT I
END SUB


11. Write a program to find the sum of factors of input number using SUB Procedure.

Program Using SUB Procedure :

DECLARE SUB FACT(N)
CLS
INPUT “Enter a Number”; N
CALL FACT(N)
END
SUB FACT(N)
FOR I = 1 TO N
IF N MOD I = 0 THEN S = S + I
NEXT I
PRINT “Sum of factors is “; S
END SUB


12. Write a program that asks any three numbers and displays the difference between the greatest and the smallest value among the 3 supplied numbers using FUNCTION procedure.

Program Using FUNCTION Procedure :

DECLARE FUNCTION DIFF(A, B, C)
CLS
INPUT “Enter the First Number”; A
INPUT “Enter the Second Number”; B
INPUT “Enter the Third Number”; C
PRINT “The Difference between the Greatest and Smallest Number is”; DIFF (A, B, C)
END
FUNCTION DIFF(A, B, C)
G = 0
S = 0
IF A > B AND A > B THEN
G = A
ELSEIF B > A AND B > C THEN
G = B
ELSE
G = C
END IF
IF A < B AND A < C THEN
S = A
ELSEIF B < A AND B < C THEN
S = B
ELSE
S = C
END IF
DIFF = G – S
END FUNCTION


13. Write a program to check whether the entered number is prime or composite.

Using SUB Procedure :

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 a Prime number”
ELSE
PRINT N; “is a Composite number”
END IF
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION PRIME(N)
CLS
INPUT “Enter any number”; N
P = PRIME(N)
IF P = 2 THEN
PRINT N; “is a Prime number”
ELSE
PRINT N; “is a 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


14. Check input number is prime or not using SUB Procedure.

Program Using SUB Procedure :

DECLARE SUB PRIME(A)
CLS
INPUT “Enter a Number”; A
CALL PRIME(A)
END
SUB PRIME(A)
FOR I = 1 TO A
IF A MOD I = 0 THEN C = C + 1
NEXT I
IF C = 2 THEN
PRINT “The given number is a Prime Number”
ELSE
PRINT “The given number is a not Prime Number”
ENDIF
END SUB


15. Display prime numbers from 1 to 500 using SUB Procedure.

Program Using SUB Procedure :

DECLARE SUB PRIME()
CLS
CALL PRIME
END
SUB PRIME
FOR I = 1 TO 500
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


16. Write a program that prints the sum of even digits.

Using 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

Using 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


17. Write a program to check input number is Armstrong or not?

Using SUB Procedure :

DECLARE SUB ARM(N)
CLS
INPUT “Enter any number”; N
CALL ARM(N)
END
SUB ARM(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; “It is an Armstrong number”
ELSE
PRINT A; “It is not an Armstrong number”
END IF
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION ARM(N)
CLS
INPUT “Enter any number”; N
A = N
AR = ARM(N)
IF A = AR THEN
PRINT A; “It is an Armstrong number”
ELSE
PRINT A; “It is not an Armstrong number”
END IF
END
FUNCTION ARM(N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 3
N = N \ 10
WEND
ARM = S
END FUNCTION


18. Write a program to display the sum of individual digits of a multi-digit input number.

Using Sub Procedure :

DECLARE SUB SDIGIT(N)
CLS
INPUT “Enter a multi-digit number”; N
CALL SDIGIT
END
SUB SDIGIT(N)
WHILE N <> 0
O = N MOD 10
SUM = SUM + O
N = N \ 10
WEND
PRINT “The sum of individual digits of supplied number is ”; SUM
END SUB

Using Function Procedure :

DECLARE FUNCTION SDIGIT(N)
CLS
INPUT “Enter a number”; N
PRINT “The total sum of individual digit is “; SDIGIT(N)
END
FUNCTION SDIGIT(N)
WHILE N<>0
P=N MOD 10
SUM=SUM+P
N=N\10
WEND
SDIGIT=SUM
END FUNCTION


19. Write a program to print the following series : 1,4,7, ………. up to 10th term using FUNCTION ………. END FUNCTION.

Program Using FUNCTION Procedure :

DECLARE FUNCTION SERIES()
CLS
D = SERIES
END
FUNCTION SERIES
A = 1
FOR I = 1 TO 10
PRINT A;
A = A + 3
NEXT I
END FUNCTION


20. Write a program to print the following series: 1, 8, 27, 64, ………. up to 10th terms using SUB procedure

Program Using SUB Procedure :

DECLARE SUB SERIES()
CLS
CALL SERIES
END
SUB SERIES
FOR I = 1 TO 10
PRINT I ^ 3;
NEXT I
END SUB


21. Write a program to display the series 55555, 5555, 555, 55, 5 using SUB ………. END SUB.

Program Using SUB Procedure :

DECLARE SUB SERIES()
CLS
CALL SERIES
END
SUB SERIES
A = 55555
FOR I = 1 TO 5
PRINT A;
A = A \ 10
NEXT I
END SUB


22. Write a program to define SUB procedure to display the following series of number: 1, 1, 2, 3, 5, ………. up to 13thterm.

Program Using SUB Procedure :

DECLARE SUB SERIES()
CLS
CALL SERIES
END
SUB SERIES
A = 1
B = 1
PRINT A; B;
FOR I = 1 TO 11
C = A + B
PRINT C;
A = A + B
B = A – B
NEXT I
END SUB


23. Write a program to generate the series using SUB ………. END SUB : -10, -8, -6, -4, ………. Up to 20th term.

Program Using SUB Procedure :

DECLARE SUB SERIES()
CLS
CALL SERIES
END
SUB SERIES
A = -10
FOR I = 1 TO 20
PRINT A;
A = A + 2
NEXT I
END SUB


24. Write a program to print string in alternate capital .eg.QbAsIc

Using SUB Procedure :

DECLARE SUB ALTERNATE(A$)
INPUT “Enter a string”; A$
END
SUB ALTERNATE(A$)
FOR I = 1 TO LEN(A$)
B$ = MID(A$, I, 1)
IF I MOD 2 = 0 THEN
B$ = LCASE$(B$)
ELSE
B$ = UCASE(B$)
END IF
ALT$ = ALT$ +B$
NEXT I “The required alternate is : “; ALT$
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION ALTERNATE$(A$)
CLS
INPUT “Enter a string”; A$
PRINT “The required alternate is : “; ALTERNATE$(A$)
END
FUNCTION ALTERNATE$(A$)
FOR I = 1 TO LEN(A$)
B$ = MID$(A$, I, 1)
IF I MOD 2 = 0 THEN
B$= LCASE$(B$)
ELSE
B$ = UCASE$(B$)
END IF
ALT$ =ALT$ +B$
NEXT I
ALTERNATE$= ALT$
END FUNCTION


25. Write a program to display vowels and consonants of the entered string by using FUNCTION ………. END FUNCTION.

Program Using FUNCTION Procedure :

DECLARE FUNCTION STR(S$)
CLS
INPUT “Enter a word”; S$
WORD = STR(S$)
END
FUNCTION STR(S$)
FOR I = 1 TO LEN(S$)
B$ = MID$(S$, I, 1)
W$ = UCASE$(B$)
IF W$ = “A” OR W$ = “E” OR W$ = “I” OR W$ = “O” OR W$ = “U” THEN
V$ = V$ + ” ” + B$
ELSE C$ = C$ + ” ” + B$
END IF
NEXT
PRINT “The vowels are : “; V$
PRINT “The consonants are : “; C$
END FUNCTION


26. Write a program to Declare SUB Procedure to print only the vowels from a given word.

Program Using SUB Procedure :

DECLARE SUB VOWELS(V$)
CLS
INPUT “Enter a word”; V$
CALL VOWELS(V$)
END
SUB VOWELS(V$)
PRINT “The vowel letters are : “
FOR I = 1 TO LEN(V$)
A$ = MID$(V$,I,1)
B$ = UCASE$(A$)
IF B$ = “A” OR B$ = “E” OR B$ = “I” OR B$ = “O” OR B$ = “U” THEN PRINT A$
NEXT I
END SUB


27. Write a program to input string and display only consonants.

Using SUB Procedure :

DECLARE SUB DISPC(S$)
CLS
INPUT “ENTER ANY STRING”; S$
CALL DISPC(S$)
END
SUB 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 SUB

Using 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


28. Write a program to input a string and count the total numbers of vowels.

Using 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 number of vowels = “; VC
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION COUNT(S$)
CLS
INPUT “Enter any string”; S$
PRINT “Total number 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


29. Write a program to display the reverse of input string.

Using 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
C$ = C$ + MID$(N$,I,1)
NEXT I
PRINT “The reversed string is”; C$
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION REV$(N$)
CLS
INPUT “Enter a string”; W$
PRINT “The reversed string is”; REV$(W$)
END
FUNCTION REV$(N$)
FOR I = LEN(N$) TO 1 STEP -1
D$ = D$ + MID$(N$,I,1)
NEXT I
REV$ = D$
END FUNCTION


30. Write a program to define a function procedure to display area of sphere where user has to input the required data in the main module. [Hint: Area of Sphere : 4*PI*R^2]

Program Using FUNCTION Procedure :

DECLARE FUNCTION AREA(R)
CLS
CONST PI = 3.14
INPUT “Enter radius”; R
PRINT “The Area of Sphere =”; AREA(R)
END
FUNCTION AREA(R)
A = 4 * PI * R ^ 2
AREA = A
END FUNCTION


31. 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%.

Program Using FUNCTION Procedure :

DECLARE FUNCTION TAX(I)
CLS
INPUT “Enter your Monthly Income”; I
PRINT “The Annual Tax to be paid is Rs. “; 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


32. Write a program using FUNCTION ………. END FUNCTION to calculate and print the total surface area of a sphere.

Program Using FUNCTION Procedure :

DECLARE FUNCTION TSA(R)
CLS
CONST PI = 3.14
INPUT “Enter the Radius of the Sphere”; R
PRINT “The Total Surface Area (T.S.A) of the Sphere is”; TSA(R)
END
FUNCTION TSA(R)
T = 4 * PI * R ^ 2
TSA = T
END FUNCTION


33. Write a program to convert the temperature given in the Centigrade to Fahrenheit.

Using SUB Procedure :

DECLARE SUB CONVERT(C)
CLS
INPUT “Enter temperature in Celsius”; C
CALL CONVERT(C)
END
SUB CONVERT(C)
F = C * (9 / 5) + 160
PRINT “Temperature in Fahrenheit is”; F
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION CONVERT(C)
CLS
INPUT “Enter temperature in Celsius”; C
PRINT “Temperature in Fahrenheit is “; CONVERT(C)
END
FUNCTION CONVERT(C)
F = C * (9 / 5) + 160
CONVERT = F
END FUNCTION


34. Write a program to count the total words present in an input string.

USING SUB PROCEDURE :

DECLARE SUB TWORDS(N$)
CLS
INPUT “Enter a string”; N$
CALL TWORDS(N$)
END
SUB TWORDS(N$)
A = 1
FOR I = 1 TO LEN(N$)
C$ = MID$(N$,I,1)
IF C$ = ” ” THEN A = A + 1
NEXT I
PRINT “Total number of words in the string is “; A
END SUB


35. Write a program to print the longest string among three different supplied string.

Using SUB Procedure :

DECLARE SUB LON(A$,B$,C$)
CLS
INPUT “Enter First String”; A$
INPUT “Enter Second String”; S$
INPUT “Enter Third String”; T$
CALL LON(A$,S$,T$)
END
SUB LON(A$,B$,C$)
A = LEN(A$)
B = LEN(B$)
C = LEN(C$)
IF A>B AND A>C THEN
LONG$ = A$
ELSEIF B>A AND B>C THEN
LONG$ = B$
ELSE
LONG$ = C$
ENDIF
PRINT “The Longest String is “; LONG$
END SUB

Using FUNCTION Procedure :

DECLARE FUNCTION LONG$(A$,B$,C$)
CLS
INPUT “Enter First String”; A$
INPUT “Enter Second String”; S$
INPUT “Enter Third String”; T$
PRINT “The Longest String is “; LONG$(A$,S$,T$)
END
FUNCTION LONG$(A$,B$,C$)
A = LEN(A$)
B = LEN(B$)
C = LEN(C$)
IF A>B AND A>C THEN
LONG$ = A$
ELSEIF B>A AND B>C THEN
LONG$ = B$
ELSE
LONG$ = C$
ENDIF
END FUNCTION


Conclusion

This was it on QBasic Programs using SUB and FUNCTION procedure. We tried solving a large number of questions with Modular programming. Hope you learned something and got helped with writing programs in SUB and FUNCTION procedure in QBasic.

By the way, did you noticed not all programs were written in both SUB and FUNCTION procedure?

I did this because I want you to try missing one yourself. Why? Because this will help you clear any doubts you might have and you can practice well.

35 programs are actually a lot and I hope you guys now know Modular Programming in QBasic with the examples i’d have given for SUB and FUNCTION procedure.

This was it! Happy programming. If in case you have any doubts, question me down in the comment section.

Subscribe to get notified of our latest posts!

Subscribe to our newsletter to get notified about the latest news at the earliest!

Leave a Reply

Your email address will not be published. Required fields are marked *