SUB Procedure QBasic Programs

Few programs for QBasic using SUB procedure. Practice some SUB procedure QBasic programs that will help you learn/understand basic concepts of modular programming in QBasic.

Content Highlights

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

Program 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

Write a program to test whether the given number is positive or negative.

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 positive”
ELSE
PRINT “The given number” N; “is negative”
END IF
END SUB

Write a program that 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

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

Program 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

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

Program 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

Write a program to print a multiplication table of any non-negative number, where the number is passed as a parameter. (Up to 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

Write a program that asks any number and calculates its factors.

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

Write a program to find the sum of factors of the input number.

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

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

Program 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

Check input number is prime or not.

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

Display prime numbers from 1 to 500.

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

Write a program that prints the sum of even digits.

QBasic Program 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

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

Program 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

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

Program 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

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

QBasic 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

Write a program to display the series 55555, 5555, 555, 55, 5

QBasic 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

Write a program print the following series of numbers: 1, 1, 2, 3, 5, ………. up to 13thterm.

QBasic 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

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

QBasic 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

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

Write a program 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

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

Program 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

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

Program 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

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

Program 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

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

Program 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

Write a program to draw/print a circle on the screen.

QBasic Program Using SUB Procedure:

DECLARE SUB CIRCLE ()
CLS
CALL CIRCLE
END
SUB CIRCLE
SCREEN 2
CIRCLE (210, 50), 100, 1
END SUB

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

Program 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

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

Program 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

Write a program to find the perimeter of a rectangle [Hint: 2(L+B)]

Program Using SUB Procedure:

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

Write a program that converts Nepalese Currency to Indian Currency

QBasic Program Using SUB Procedure:

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

Write a program to find the Highest Common Factor (HCF)

QBasic Program Using SUB Procedure:

DECLARE SUB HCFAC ()
CLS
CALL HCFAC
END
SUB HCFAC
INPUT “Enter the first number”; A
INPUT “Enter the second number”; B
N = A
N = B
WHILE A <> 0
R = B MOD A
B = A
A = R
WEND
PRINT “The Highest Common Factor (HCF) is ”; B
END SUB

Write a program to supply percentages from the keyboard and print grades by following the conditions provided to display the result.

Percentage Grade Table:

Obtained PercentageGrade
<40Fail
=40 to <60C
=60 to <80B
=80 to <=100A

QBasic Program Using SUB Procedure:

DECLARE SUB GRADE (M)
CLS
INPUT “Enter the obtained percentage”; M
CALL GRADE(M)
END
SUB GRADE (M)
SELECT CASE M
CASE IS < 40
R$ = “Fail”
CASE IS <= 60
R$ = “Grade C”
CASE IS <= 80
R$ = “Grade B”
CASE IS <= 100
R$ = “Grade A”
END SELECT
PRINT “The result is ”; R$
END SUB