Here are 10 QBasic problems using function procedures with similar types of tasks. I’ve avoided the use of DIM keyword as you requested.
Lab Work Questions and Programs on Modular Programming with Function Procedures in QBasic
1. Calculate the Area of a Rectangle
Question:
Write a QBasic program that calculates the area of a rectangle using a function procedure. The user will input the length and width of the rectangle.
DECLARE FUNCTION Area (length AS SINGLE, width AS SINGLE) AS SINGLE
' Ask user for input
PRINT "Enter the length of the rectangle:"
INPUT length
PRINT "Enter the width of the rectangle:"
INPUT width
' Call the function to calculate area and display the result
PRINT "The area of the rectangle is: "; Area(length, width)
' Function to calculate the area of the rectangle
FUNCTION Area (length AS SINGLE, width AS SINGLE) AS SINGLE
Area = length * width
END FUNCTION
2. Calculate the Volume of a Rectangular Box
Question:
Write a QBasic program that calculates the volume of a rectangular box using a function procedure. The user will input the length, width, and height.
DECLARE FUNCTION Volume (length AS SINGLE, width AS SINGLE, height AS SINGLE) AS SINGLE
' Ask user for input
PRINT "Enter the length of the box:"
INPUT length
PRINT "Enter the width of the box:"
INPUT width
PRINT "Enter the height of the box:"
INPUT height
' Call the function to calculate volume and display the result
PRINT "The volume of the box is: "; Volume(length, width, height)
' Function to calculate the volume of the box
FUNCTION Volume (length AS SINGLE, width AS SINGLE, height AS SINGLE) AS SINGLE
Volume = length * width * height
END FUNCTION
3. Count the Number of Vowels in a Word
Question:
Write a QBasic program that counts the number of vowels in a word using a function procedure. The user will input a word.
DECLARE FUNCTION CountVowels (word AS STRING) AS INTEGER
' Ask user for input
PRINT "Enter a word:"
INPUT word
' Call the function to count vowels and display the result
PRINT "The number of vowels in the word is: "; CountVowels(word)
' Function to count vowels
FUNCTION CountVowels (word AS STRING) AS INTEGER
COUNT = 0
FOR i = 1 TO LEN(word)
IF INSTR("aeiouAEIOU", MID$(word, i, 1)) > 0 THEN
COUNT = COUNT + 1
END IF
NEXT i
CountVowels = COUNT
END FUNCTION
4. Count the Number of Consonants in a Word
Question:
Write a QBasic program that counts the number of consonants in a word using a function procedure. The user will input a word.
DECLARE FUNCTION CountConsonants (word AS STRING) AS INTEGER
' Ask user for input
PRINT "Enter a word:"
INPUT word
' Call the function to count consonants and display the result
PRINT "The number of consonants in the word is: "; CountConsonants(word)
' Function to count consonants
FUNCTION CountConsonants (word AS STRING) AS INTEGER
COUNT = 0
FOR i = 1 TO LEN(word)
IF INSTR("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ", MID$(word, i, 1)) > 0 THEN
COUNT = COUNT + 1
END IF
NEXT i
CountConsonants = COUNT
END FUNCTION
5. Calculate the Sum of Two Numbers
Question:
Write a QBasic program that calculates the sum of two numbers using a function procedure. The user will input two numbers.
DECLARE FUNCTION Sum (num1, num2)
' Ask user for input
PRINT "Enter the first number:"
INPUT num1
PRINT "Enter the second number:"
INPUT num2
' Call the function to calculate sum and display the result
PRINT "The sum of the two numbers is: "; Sum(num1, num2)
' Function to calculate the sum of two numbers
FUNCTION Sum (num1, num2)
Sum = num1 + num2
END FUNCTION
6. Calculate the Factorial of a Number
Question:
Write a QBasic program that calculates the factorial of a number using a function procedure. The user will input a number.
DECLARE FUNCTION Factorial (n )
' Ask user for input
PRINT "Enter a number to calculate its factorial:"
INPUT n
' Call the function to calculate factorial and display the result
PRINT "The factorial of "; n; " is: "; Factorial(n)
' Function to calculate the factorial of a number
FUNCTION Factorial (n)
IF n = 0 OR n = 1 THEN
Factorial = 1
ELSE
Factorial = n * Factorial(n - 1)
END IF
END FUNCTION
7. Calculate the Average of Three Numbers
Question:
Write a QBasic program that calculates the average of three numbers using a function procedure. The user will input three numbers.
DECLARE FUNCTION Average (num1, num2, num3)
' Ask user for input
PRINT "Enter the first number:"
INPUT num1
PRINT "Enter the second number:"
INPUT num2
PRINT "Enter the third number:"
INPUT num3
' Call the function to calculate average and display the result
PRINT "The average of the numbers is: "; Average(num1, num2, num3)
' Function to calculate the average of three numbers
FUNCTION Average (num1, num2, num3)
Average = (num1 + num2 + num3) / 3
END FUNCTION
8. Convert Temperature from Celsius to Fahrenheit
Question:
Write a QBasic program that converts temperature from Celsius to Fahrenheit using a function procedure. The user will input the temperature in Celsius.
DECLARE FUNCTION CelsiusToFahrenheit (celsius )
' Ask user for input
PRINT "Enter temperature in Celsius:"
INPUT celsius
' Call the function to convert Celsius to Fahrenheit and display the result
PRINT "The temperature in Fahrenheit is: "; CelsiusToFahrenheit(celsius)
' Function to convert Celsius to Fahrenheit
FUNCTION CelsiusToFahrenheit (celsius)
CelsiusToFahrenheit = (celsius * 9/5) + 32
END FUNCTION
9. Convert Temperature from Fahrenheit to Celsius
Question:
Write a QBasic program that converts temperature from Fahrenheit to Celsius using a function procedure. The user will input the temperature in Fahrenheit.
DECLARE FUNCTION FahrenheitToCelsius (fahrenheit)
' Ask user for input
PRINT "Enter temperature in Fahrenheit:"
INPUT fahrenheit
' Call the function to convert Fahrenheit to Celsius and display the result
PRINT "The temperature in Celsius is: "; FahrenheitToCelsius(fahrenheit)
' Function to convert Fahrenheit to Celsius
FUNCTION FahrenheitToCelsius (fahrenheit)
FahrenheitToCelsius = (fahrenheit - 32) * 5/9
END FUNCTION
10. Reverse a String
Question:
Write a QBasic program that reverses a string using a function procedure. The user will input a string.
DECLARE FUNCTION ReverseString (str )
' Ask user for input
PRINT "Enter a string:"
INPUT str
' Call the function to reverse the string and display the result
PRINT "The reversed string is: "; ReverseString(str)
' Function to reverse the string
FUNCTION ReverseString (str)
ReverseString = ""
FOR i = LEN(str) TO 1 STEP -1
ReverseString = ReverseString + MID$(str, i, 1)
NEXT i
END FUNCTION
Conclusion:
These programs teach modular programming in QBasic using function procedures. They are simple to advanced, and cover various topics such as mathematical operations, string manipulations, and conversions. By solving these problems, students can practice breaking tasks into smaller functions and learn how to pass and return values from them effectively.