In QBASIC, the SUB procedure is a powerful tool for organizing code and making it more manageable. It allows you to define a block of code that can be called from different parts of your program, reducing redundancy and improving readability. This article will explore how to use SUB procedures specifically for generating series, which is a common requirement in programming tasks.
Understanding SUB Procedure:
A SUB procedure in QBASIC is a named block of code that performs a specific task. It can take parameters and return values if necessary. Using SUB procedures can help break down complex problems into smaller, more manageable parts, making your code easier to understand and maintain.
Generating Series using SUB Procedure:
One common application of SUB procedures is generating series of numbers. Let's look at a simple example of generating the Fibonacci series using a SUB procedure:
Very Important End..Sub QBASIC Programming(Turn on Desktop Mode for Better View)
1. Fibonacci series i.e. 0 1 1 2 3 5 8 ..............20th term
DECLARE SUB Series()
CLS
CALL Series
END
SUB Series
PRINT "Series up to the 10th term:"
a = 1
PRINT a;
b = 1
FOR i = 2 TO 10
c = a + b
PRINT c;
a = b
b = c
NEXT
END SUB
2. 1 2 3 6 11 20 ….10th term
DECLARE SUB Series()
CLS
CALL Series
END
SUB Series
PRINT "Series up to the 10th term:"
a = 1
PRINT a;
b = 1
FOR i = 2 TO 10
c = a + b
PRINT c;
a = b
b = c
NEXT
END SUB
3. 1 2 4 7 11 ...............10th term
DECLARE SUB GenerateSeries()
CLS
CALL GenerateSeries
END
SUB GenerateSeries
PRINT "Series up to the 10th term:"
a = 1
PRINT a;
b = 1
FOR i = 2 TO 10
c = a + (i - 1)
PRINT c;
a = c
NEXT
END SUB
4. 1 2 5 10 17 ..............10th term
DECLARE SUB GenerateSeries()
CLS
CALL GenerateSeries
END
SUB GenerateSeries
PRINT "Series up to the 10th term:"
a = 1
PRINT a;
b = 1
FOR i = 2 TO 10
c = a + (i - 1) * 3
PRINT c;
a = c
NEXT
END SUB
5. 3 12 27 48 ................10th term
DECLARE SUB GenerateSeries()
CLS
CALL GenerateSeries
END
SUB GenerateSeries
PRINT "Series up to the 10th term:"
a = 3
PRINT a;
FOR i = 2 TO 10
c = i * (i + 1) * 3
PRINT c;
NEXT
END SUB
6. 1 5 25 125 ...........7th term
DECLARE SUB GenerateSeries()
CLS
CALL GenerateSeries
END
SUB GenerateSeries
PRINT "Series up to the 7th term:"
a = 1
PRINT a;
FOR i = 2 TO 7
c = a * 5
PRINT c;
a = c
NEXT
END SUB
7. 5 10 20 35 55 ................10th term
DECLARE SUB GenerateSeries()
CLS
CALL GenerateSeries
END
SUB GenerateSeries
PRINT "Series up to the 10th term:"
a = 5
PRINT a;
b = 5
FOR i = 2 TO 10
c = a + b
PRINT c;
a = c
b = b + 5
NEXT
END SUB
8. To convert given second into respective hour, minute and second.(using only Sub....End Sub.)
DECLARE SUB ConvertTime(seconds)
CLS
INPUT "Enter seconds: "; seconds
CALL ConvertTime(seconds)
END
SUB ConvertTime(seconds)
hours = seconds \ 3600
remaining_seconds = seconds MOD 3600
minutes = remaining_seconds \ 60
remaining_seconds = remaining_seconds MOD 60
PRINT "Hours: "; hours; ", Minutes: "; minutes; ", Seconds: "; remaining_seconds
END SUB
9. To display and count all the vowels and consonant from the given string.(using only Sub....End Sub.)
DECLARE SUB CountVowelsAndConsonants(inputString)
CLS
INPUT "Enter a string: "; inputString
CALL CountVowelsAndConsonants(inputString)
END
SUB CountVowelsAndConsonants(inputString)
vowels = 0
consonants = 0
inputString = UCASE$(inputString)
FOR i = 1 TO LEN(inputString)
char = MID$(inputString, i, 1)
IF char >= "A" AND char < = "Z" THEN
IF INSTR("AEIOU", char) THEN
vowels = vowels + 1
ELSE
consonants = consonants + 1
END IF
END IF
NEXT
PRINT "Vowels: "; vowels; ", Consonants: "; consonants
END SUB
10. To display all the factors of given number.(using only Sub....End Sub.)
DECLARE SUB DisplayFactors(number)
CLS
INPUT "Enter a number: "; number
CALL DisplayFactors(number)
END
SUB DisplayFactors(number)
PRINT "Factors of "; number; ":"
FOR i = 1 TO number
IF number MOD i = 0 THEN
PRINT i;
END IF
NEXT
END SUB
11. To display first 10 multiples of given number.(using only Sub....End Sub.)
DECLARE SUB DisplayMultiples(number)
CLS
INPUT "Enter a number: "; number
CALL DisplayMultiples(number)
END
SUB DisplayMultiples(number)
PRINT "First 10 multiples of "; number; ":"
FOR i = 1 TO 10
PRINT number * i;
NEXT
END SUB
12. To check if a given number is a prime number.(using only Sub....End Sub.)
ECLARE SUB CheckPrime(number)
CLS
INPUT "Enter a number: "; number
CALL CheckPrime(number)
END
SUB CheckPrime(number)
prime = 1
IF number <= 1 THEN
prime = 0
ELSE
FOR i = 2 TO number \ 2
IF number MOD i = 0 THEN
prime = 0
EXIT FOR
END IF
NEXT
END IF
IF prime THEN
PRINT number; " is a prime number."
ELSE
PRINT number; " is not a prime number."
END IF
END SUB
13. To reverse a given string.(using only Sub....End Sub.)
DECLARE SUB ReverseString(inputString)
CLS
INPUT "Enter a string: "; inputString
CALL ReverseString(inputString)
END
SUB ReverseString(inputString)
reversedString = ""
FOR i = LEN(inputString) TO 1 STEP -1
reversedString = reversedString + MID$(inputString, i, 1)
NEXT
PRINT "Reversed string: "; reversedString
END SUB
14. To calculate the factorial of a given number.(using only Sub....End Sub.)
DECLARE SUB CalculateFactorial(number)
CLS
INPUT "Enter a number: "; number
CALL CalculateFactorial(number)
END
SUB CalculateFactorial(number)
factorial = 1
FOR i = 1 TO number
factorial = factorial * i
NEXT
PRINT "Factorial of "; number; " is "; factorial
END SUB
15. To check if a given string is a palindrome.(using only Sub....End Sub.)
DECLARE SUB CheckPalindrome(inputString)
CLS
INPUT "Enter a string: "; inputString
CALL CheckPalindrome(inputString)
END
SUB CheckPalindrome(inputString)
reversedString = ""
FOR i = LEN(inputString) TO 1 STEP -1
reversedString = reversedString + MID$(inputString, i, 1)
NEXT
IF UCASE$(inputString) = UCASE$(reversedString) THEN
PRINT inputString; " is a palindrome."
ELSE
PRINT inputString; " is not a palindrome."
END IF
END SUB
16. To calculate the sum of digits of a given number.(using only Sub....End Sub.)
DECLARE SUB SumOfDigits(number)
CLS
INPUT "Enter a number: "; number
CALL SumOfDigits(number)
END
SUB SumOfDigits(number)
sum = 0
WHILE number > 0
digit = number MOD 10
sum = sum + digit
number = number \ 10
WEND
PRINT "Sum of digits: "; sum
END SUB
17. To find the largest element in an array.(using only Sub....End Sub.)
DECLARE SUB FindLargestElement(array(), size)
CLS
DIM array(5)
FOR i = 1 TO 5
INPUT "Enter element "; i; ": "; array(i)
NEXT
CALL FindLargestElement(array(), 5)
END
SUB FindLargestElement(array(), size)
largest = array(1)
FOR i = 2 TO size
IF array(i) > largest THEN
largest = array(i)
END IF
NEXT
PRINT "Largest element in the array: "; largest
END SUB

If you have any doubts, Please let me know