Qbasic looping with sub procedure
QBasic is an
interpreted programming language that was widely used in the 1980s and 1990s.
One of the most important features of QBasic is its looping capability, which
allows programmers to execute a set of statements repeatedly until a certain
condition is met. In this article, we will discuss QBasic looping with sub procedure, which is a powerful technique to enhance code readability and
maintainability.
Sub procedure in QBasic
Before we dive into
looping with sub procedure, let's discuss what sub procedure are in QBasic. Sub..End
sub also called subroutine. It is also known as procedures or functions, are
self-contained blocks of code that perform a specific task. Sub procedure allow
programmers to break down a large program into smaller, more manageable pieces.
In QBasic, sub procedure are declared using the "SUB" keyword,
followed by the subroutine name and any necessary parameters, and then ended
with the "END SUB" keyword.
Here is an example of a
sub procedurein QBasic:
SUB
PrintMessage(Message AS String)
PRINT Message
END
SUB
Above subroutine, named
"PrintMessage," takes one parameter, "Message," which is a
string that will be printed to the console when the subroutine is called. To
call this subroutine from another part of the program, you would simply use the
subroutine name followed by the necessary arguments, like this:
PrintMessage("Hello,
world!")
This would output the
message "Hello, world!" to the console.
Qbasic Looping with Sub procedure
This
subroutine, named "PrintNumbers," uses a "FOR" loop to
iterate from 1 to 10 and print each number to the console. To call this
subroutine from another part of the program, you would simply use the
subroutine name, like this:
PrintNumbers()
This would output the
numbers 1 through 10 to the console.
Looping in qbasic
A loop is defined as a set of instructions
that allows us to repeat the block of statements to the given number of times
or till the given condition is satisfied or until a certain condition becomes
true. Programming process jumps forward and backward in looping. Iteration is
another name for looping.
Every
loop statement requires following terms:
Counter:
A
counter is a variable which controls the loop statement and provides the track
to run the loop statement in certain number of times.
Initial
Value:
The
starting value of a loop statement is called initial value. Initial value may
be negative, zero or positive.
Sentinel
Value:
The
final (last) value of loop statement after which the loop will terminate.
Step
Value:
Step
value is used to modify the value of counter by a certain amount. If initial
value is greater than sentinel value, step value will be negative. If initial
value is less than sentinel value, step value will be positive. Step value
never be neutral or zero.
Accumulator:
Accumulator
is a numeric variable which accumulate the values in a loop statement.
Following
are the different types of looping in qbasic
i)
FOR……NRXT
ii)
WHILE……WEND
iii)
DO……LOOP
FOR…NEXT
It
is a controlled looping statement in which the statements are executed for a
given number of times. The FOR….NEXT looping uses a variable called counter
that modifies the value during each repetition of the loop.
The
loop is executed with the loop control variable at initial value to final value
and the values in between.
Syntax:
FOR
COUNTER = START TO END [STEP value]
[statements]
NEXT
Where,
COUNTER is a numeric
variable used as a loop counter. It is also called control variable.
START is a initial value of the
counter variable.
END is the final or sentinel
value of the counter variable.
STEP is an optional which is
used to incensement or decrement the value of counter variable by a certain
value.
Example: CLS REM program to display the natural number from 1 to 5 vertically FOR x = 1 TO 5 PRINT x NEXT x END |
Output: 1 2 3 4 5
|
Note: Use
(;) in print statement to display the result in horizontally.
WHILE…..WEND
It
is a loop statement that executes a series of statement in a loop as long as a
given condition is true.
Syntax:
WHILE
<condition>
[statement
blocks]
………………..
WEND
Where,
Condition it is any Boolean
expression that gives true or false as result.
Statement
block are
the list of statements that will be executed if the condition is true.
Example:
1
CLS
REM
program to display the natural number from 1 to 50 vertically
x
= 1
WHILE
x <= 50
PRINT
x
x
= x + 1
WEND
END
DO ……LOOP
It
is a loop statement that repeats a block of statement while a condition is true
or until a condition becomes true. This loop statement can execute either WHILE
or UNTIL condition becomes true
Syntax: 1 DO [{WHILE|UNTIL} <condition>] [statement blocks] LOOP |
Syntax: 2 DO [Statement blocks] LOOP [{WHILE|UNTIL} <condition> ] |
Example:1 Using DO WHILE …..LOOP REM program to display the natural number from 1 to 10 CLS x = 1 DO WHILE x <= 10 PRINT x x = x + 1 LOOP END |
Example: 2 Using: DO UNTIL ……LOOP REM program to display the natural number from 1 to 10 CLS x = 1 DO UNTIL x > 10 PRINT x x = x + 1 LOOP END |
Solved examples of qbasic looping with sub procedure
1.
WAP to display the series of number from 1 to 10.
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
FOR
x= 1 to 10
PRINT
x
NEXT
x
END
SUB
2.
WAP to print the following serial 9,7,5…….1 (SLC 2065 )
DECLARE
SUB series( )
CALL
series
END
SUB
series
FOR
n= 9 to 1 STEP -2
PRINT
n;
NEXT
n
3.
WAP to display the even number from 2 to 20 and their sum.
DECLARE
SUB sum( )
CLS
CALL
sum
END
SUB
sum
FOR
x= 2 to 20 STEP 2
PRINT
x
s=s+x
NEXT
x
PRINT
s
4.
WAP to display the series like 5 55 555 5555 55555.
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=5
FOR
x= 1 to 5
PRINT
a;
a=(a*10)+5
NEXT
x
END
SUB
5.
WAP to display the series like 55555 5555 555 55 5
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=55555
x=1
WHILE
x<=5
PRINT
a;
a=a\10
x=x+1
WEND
6.
WAP to display the “BASIC” 50 times on the screen
DECLARE
SUB basic(a$)
CLS
a$=
“QBASIC”
CALL
basic(a$)
END
SUB
basic(a$)
FOR
x= 1 to 50
PRINT
a$
NEXT
END
SUB
7.
WAP to display the series 1,8,27…… till 10th term.
DECLARE
SUB out( )
CLS
CALL
out
END
SUB
out
x=1
WHILE
x<=10
PRINT
x^3
x=x+1
WEND
END
SUB
8.
WAP to display the series 2,8,18,32….10th term.
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
FOR
x= 1 to 10
PRINT
(x^2)*2;
NEXT
x
END
SUB
9.
WAP to display the series 1,8,27,……10th term.
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=1
DO
PRINT
a^3
a=a+1
LOOP
WHILE a<=10
END
SUB
10
Write a program using to display given series up to 15th term
525, 500, 475, 450
DECLARE
SUB ser ()
CLS
CALL
ser
END
SUB
ser
a
= 525
FOR
x = 1 TO 15
PRINT
a;
a
= a - 25
NEXT
x
END
SUB
11.
WAP to display the series as : 5 6 9 14 21 30
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=5:
b=1
PRINT
a;
FOR
x= 1 to 5
c=a+b
PRINT
c;
a=c
b=b+2
NEXT
x
END
SUB
12.
WAP to display the series as 8 18 32 50 72 98……. 10th term
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=2:
b=6
FOR
x= 1 to 10
c=a+b
PRINT
c;
a=c
b=b+4
NEXT
x
END
SUB
13.
WAP to display the series as 3 12 27 ……10th term.
DECLARE
SUB ser( )
CLS
CALL
ser
END
SUB
ser
a=3
FOR
x= 1 to 10
PRINT
a*x;
a=a+3
NEXT
x
END
SUB
14.
WAP to display the multiplication table of 5.
DECLARE
SUB mul( )
CLS
CALL
mul
END
SUB
mul
a=5
FOR
x= 1 to 10
PRINT
a; "X"; x; "=";a*x
NEXT
x
END
SUB
15.
Write a program using a SUB procedure module to print the multiplication table
of any input number up to 10th term. (SEE 2075 State 2)
DECLARE
SUB MUL (N)
CLS
INPUT
"ENTER ANY NUMBER"; N
CALL
MUL (N)
END
SUB
MUL (N)
FOR
I = 1 TO 10
PRINT
N; "X"; I; "="; N * I
NEXT
I
END
SUB
16.
WAP to input any string from the user and display it in reverse order. (SEE 2072)
DECLARE
SUB r(w$)
CLS
INPUT
"Enter any string:-";w$
CALL
r(w$)
END
SUB
r(w$)
FOR
x= LEN(w$) to 1 STEP -1
PRINT
MID$(w$,x,1);
NEXT
x
END
SUB
17.
WAP to display all the vowel letters from the input string.
DECLARE
SUB COUNT (S$)
CLS
INPUT
"ENTER ANY STRING"; S$
CALL
COUNT(S$)
END
SUB
COUNT (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 C$
NEXT
I
END
SUB
18.
WAP to print the total number of vowel alphabets present in the given
word. (SEE
2075)
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
19.
WAP to display only consonant letters from the given string.
DECLARE
SUB test ()
CLS
CALL
test
END
SUB
test
INPUT
"enter any string"; w$
p$
= UCASE$(w$)
FOR
x = 1 TO LEN(p$)
m$
= MID$(p$, x, 1)
IF
m$ <> "A" AND m$ <> "E" AND m$ <>
"I" AND m$ <> "O" AND m$ <> "U" THEN
PRINT m$
NEXT
END
SUB
20.
WAP to count total numbers of consonant letters from input string.
DECLARE
SUB test ()
CLS
CALL
test
END
SUB
test
INPUT
"enter any string"; w$
p$
= UCASE$(w$)
FOR
x = 1 TO LEN(p$)
m$
= MID$(p$, x, 1)
IF
m$ <> "A" AND m$ <> "E" AND m$ <> "I"
AND m$ <> "O" AND m$ <> "U" THEN c=c+1
NEXT
PRINT
“Total consonant letters”;c
END
SUB
21
. WAP to input any string from the keyboard and display whether
that string is palindrome or not example of
palindrome is LIRIL. It is same from left to right
and right to left.
DECLARE
SUB p(w$)
CLS
INPUT
"Enter any string:";w$
CALL
p(w$)
END
SUB
p(w$)
FOR
x= LEN(w$) to 1 STEP -1
m$=m$+MID$(w$,x,1)
NEXT
x
IF
w$=m$ THEN
PRINT
"Palindrome"
ELSE
PRINT
"Not Palindrome"
ENDIF
END
SUB
22. WAP
to input any string from the keyboard and display how many words are available
in string. (SEE 2074)
DECLARE
SUB word(w$)
CLS
INPUT
"Enter any string:";w$
CALL
word(w$)
w=1
FOR
x= 1 to LEN(w$)
m$=MID$(w$,x,1)
IF
m$=" " THEN
w=w+1
ENDIF
NEXT
x
PRINT
"Total no of words=";w
END
SUB
23. WAP
to input any word from the keyboard and count total occurrences of
the character R ignoring uppercase and lowercase.
DECLARE
SUB res ()
CLS
CALL
res
END
SUB
res
CLS
INPUT
"enter any sentence="; a$
INPUT
"Enter the character which you want to count.="; b$
FOR
x = 1 TO LEN(a$)
c$
= MID$(a$, x, 1)
IF
b$ = c$ THEN r = r + 1
NEXT
x
PRINT
"Total no of character available="; r
END
SUB
24. WAP
to input your full name and display initial letters. Eg. If you
input name as Govind Prasad Joshi the initial letters are G.P. Joshi
DECLARE
SUB nam ()
CLS
CALL
nam
END
SUB
nam
INPUT
"Enter your name="; n$
FOR
x = 1 TO LEN(n$)
IF
MID$(n$, x, 1) = " " THEN
PRINT
MID$(n$, p + 1, 1); ".";
p
= x
END
IF
NEXT
x
PRINT
RIGHT$(n$, LEN(n$) - p)
END
SUB
25.
Write a program to print input string in alternate capital. Eg. CoMpUtEr
DECLARE
SUB mysub(a$)
CLS
INPUT
“Enter a number”; a$
CALL
mysub(a$)
END
SUB
mysub(a$)
FOR
i = 1 to len(a$)
b$=
mid$(a$,i,1)
c=
i MOD 2
IF
c<>0 THEN c$=UCASE(b$)
ELSE
c$=LCASE(b$)
ENDIF
d$=d$+c$
NEXT
i
PRINT
d$
END
SUB
26.
WAP print the fibbonies series 1,1,2,3,5,8…..upto ten terms. (SLC 2069)
DECLARE
SUB fibo ()
CLS
CALL
fibo
END
SUB
fibo
a
= 1
b
= 1
FOR
i = 1 TO 5
PRINT
a; b;
a
= a + b
b
= a + b
NEXT
27.
WAP to input any digit and display its sum. (Example: If user inputs digit 123
then output will be 1+2+3=6]
DECLARE
SUB sum ()
CLS
CALL
sum
END
SUB
sum
INPUT
"enter any no"; n
WHILE
n <> 0
r
= n MOD 10
s
= s + r
n
= n \ 10
WEND
PRINT
s
END
SUB
28. WAP
to display the input digit in reverse order. (Example: if user input digit
123 then output will be 321)
DECLARE
SUB sum ()
CLS
CALL
sum
END
SUB
sum
INPUT
"enter any no"; n
WHILE
n <> 0
r
= n MOD 10
s
= s * 10 + r
n
= n \ 10
WEND
PRINT
s
END
SUB
29.
WAP to check whether input digit is palindrome or not. (Palindrome is the
digit that looks like same from both side)
DECLARE
SUB sum ()
CLS
CALL
sum
END
SUB
sum
INPUT
"enter any no"; n
m
= n
WHILE
n <> 0
r
= n MOD 10
s
= s * 10 + r
n
= n \ 10
WEND
IF
m = s THEN
PRINT
"Digit is palindrome"
ELSE
PRINT
"Digit is not palindrome"
END
IF
END
SUB
30.
WAP to check whether input digit is 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. For example: 371 is an Armstrong number since
3^3+7^3+1^3=371)
DECLARE
SUB arm ()
CLS
CALL
arm
END
SUB
arm
INPUT
"enter any digit"; n
m
= n
WHILE
n <> 0
r
= n MOD 10
s
= s + r ^ 3
n
= n \ 10
WEND
IF
s = m THEN
PRINT
"Armstrong number"
ELSE
PRINT
"not Armstrong number"
END
IF
END
SUB
31.
WAP to find the product of the digits of the given number.
DECLARE
SUB PD(N)
CLS
INPUT
"Enter a Number";N
CALL
PD(N)
END
SUB
PD(N)
R
= 1
WHILE
N<>0
A
= N MOD 10
R
= R * A
N
= FIX ( N / 10 )
WEND
PRINT
"Product of Digit is";R
END
SUB
32.
Write a program that displays factorial of an input number using
DECLARE
SUB FACT(N)
CLS
INPUT
“ENTER ANY NUMBER”;N
CALL
FACT(N)
END
SUB
FACT(N)
F=1
FOR
I = 1 TO N
F=F*I
NEXT
I
PRINT
“FACTORIAL OF”;N;”=”;F
END
SUB
33.
WAP to input any number and display whether input number is prime or composite.
DECLARE
SUB prime ()
CLS
CALL
prime
END
SUB
prime
INPUT
"enter any no"; n
c
= 0
FOR
x = 1 TO n
IF
n MOD x = 0 THEN c = c + 1
NEXT
IF
c = 2 THEN
PRINT
"prime no"
ELSE
PRINT
"Composite no"
ENDIF
END
SUB
34.
WAP to display the sum of even digit of input digit.
Conclusion
In this blog post, we
discussed QBasic looping with sub procedure, which is a powerful technique to enhance
code readability and maintainability. We also discussed sub procedure in QBasic,
which are self-contained blocks of code that perform a specific task. Subroutines or sub procedures allow programmers to break down a large program into smaller, more manageable
pieces.
We learned about
different types of looping in QBasic, such as FOR…NEXT, WHILE…WEND, and
DO…LOOP. We also saw examples of each of these looping structures.
In summary, QBasic
looping with sub procedure is an essential technique for any QBasic programmer.
It helps to make the code more modular and easier to read and maintain. With
this knowledge, you can now write more efficient and effective QBasic programs.
You may also Read:
QBASIC program to check whether the input number is positive, negative, or neutral.
No comments:
Post a Comment