Lab Work Questions on User-Defined Functions in QBasic
Question 1: Write a program using a user-defined function to calculate the area of a rectangle.
Answer:
DECLARE FUNCTION Aare(length, breadth)
CLS
REM Main Program
INPUT "Enter the length of the rectangle: ", length
INPUT "Enter the breadth of the rectangle: ", breadth
REM Call the function
result = Area(length, breadth)
PRINT "The area of the rectangle is: "; result
END
REM Define the function to calculate area
FUNCTION Area(length, breadth)
Area = length * breadth
END FUNCTION
Explanation:
- A function
Area
is defined to calculate the area of a rectangle using the formulaArea = length * breadth
. - The function is called in the main program to compute and print the area.
Question 2: Write a program using a user-defined function to calculate the perimeter of a rectangle.
Answer:
DECLARE FUNCTION Perimeter (length, breadth)
CLS
REM Main Program
INPUT "Enter the length of the rectangle: ", length
INPUT "Enter the breadth of the rectangle: ", breadth
REM Call the function
result = Perimeter(length, breadth)
PRINT "The perimeter of the rectangle is: "; result
END
REM Define the function to calculate perimeter
FUNCTION Perimeter(length, breadth)
Perimeter = 2 * (length + breadth)
END FUNCTION
Explanation:
- The
Perimeter
function calculates the perimeter using the formula2 * (length + breadth)
. - The function is then called to compute and display the perimeter.
Question 3: Write a program using a user-defined function to check if a number is a palindrome.
Answer:
DECLARE Function Ispalindrome$(number)
CLS
REM Main Program
INPUT "Enter a number: ", num
REM Call the function
PRINT num; Ispalindrome$
END
REM Define the function to check palindrome
FUNCTION IsPalindrome(number)
original = number
reverse = 0
WHILE number <> 0
remainder = number MOD 10
reverse = reverse * 10 + remainder
number = INT(number / 10)
WEND
IF original = reverse THEN
IsPalindrome$ = "Palindrome"
ELSE
IsPalindrome$ = "Not palindrome"
END IF
END FUNCTION
Explanation:
- The function
IsPalindrome
checks if the number is the same when reversed. If true, it returnsTRUE
; otherwise, it returnsFALSE
. - The main program calls this function and displays the result.
Question 4: Write a program using a user-defined function to check if a string is a palindrome.
Answer:
DECLARE FUNCTION IsStringPalindrome$(str$)
CLS
REM Main Program
INPUT "Enter a string: ", userString
REM Call the function
PRINT userString; IsStringPalindrome$;
END
REM Define the function to check palindrome string
FUNCTION IsStringPalindrome$(str$)
len = LEN(str$)
FOR i = 1 TO len
rev$=UCASE$(MID$(str$,i,1)+rev$
NEXT i
IF str$=rev$ THEN
IsStringPalindrome$=" String Palindrome"
ELSE
IsStringPalindrome$=" String not Palindrome"
END IF
END FUNCTION
Explanation:
- The function
IsStringPalindrome
checks if the string is the same when read forwards and backwards. - The program calls this function and prints whether the string is a palindrome.
Question 5: Write a program using a user-defined function to find the factorial of a number.
Answer:
DECLARE FUNCTION Factorial(n)
CLS
REM Main Program
INPUT "Enter a number: ", n
REM Call the function
result = Factorial(n)
PRINT "The factorial of "; n; " is: "; result
END
REM Define the function to calculate factorial
FUNCTION Factorial(num)
fact = 1
FOR i = 1 TO num
fact = fact * i
NEXT i
Factorial = fact
END FUNCTION
Explanation:
- The function
Factorial
calculates the factorial of a number using aFOR-NEXT
loop. - The main program calls this function to calculate and display the factorial.
Question 6: Write a program using a user-defined function to check whether a number is even or odd.
Answer:
DECLARE FUNCTION IsEvenOrOdd$(number)
CLS
REM Main Program
INPUT "Enter a number: ", number
REM Call the function
result$ = IsEvenOrOdd$(number)
PRINT "The number "; number; " is "; result$
END
REM Define the function to check even or odd
FUNCTION IsEvenOrOdd(num)
IF num MOD 2 = 0 THEN
IsEvenOrOdd$ = "Even"
ELSE
IsEvenOrOdd$ = "Odd"
END IF
END FUNCTION
Explanation:
- The function
IsEvenOrOdd
checks if the number is divisible by 2 (MOD 2 = 0
) to determine if it's even or odd. - The main program calls the function and displays whether the number is even or odd.
Summary of User-Defined Functions:
Area
function: Calculates the area of a rectangle.Perimeter
function: Calculates the perimeter of a rectangle.IsPalindrome
function: Checks if a number is a palindrome.IsStringPalindrome
function: Checks if a string is a palindrome.Factorial
function: Calculates the factorial of a number.IsEvenOrOdd
function: Checks if a number is even or odd.
These questions are designed to help students practice creating and using user-defined functions in QBasic to solve various problems.
No comments:
Post a Comment