Modular Programming Using Sub-Procedures in QBASIC
1. Program to Calculate Area and Volume Using Sub-Procedures
Program:
DECLARE SUB CalculateArea(radius)
DECLARE SUB CalculateVolume(radius)
CLS
REM Main program
PRINT "Enter the radius of the circle/sphere: ";
INPUT radius
CALL CalculateArea(radius)
CALL CalculateVolume(radius)
END
REM Sub-procedure to calculate area of a circle
SUB CalculateArea (radius)
area = 3.14159 * radius * radius
PRINT "The area of the circle is: "; area
END SUB
REM Sub-procedure to calculate volume of a sphere
SUB CalculateVolume (radius)
volume = (4 / 3) * 3.14159 * radius * radius * radius
PRINT "The volume of the sphere is: "; volume
END SUB
2. Program to Check if Entered Number is Palindrome Using Sub-Procedure
Program:
DECLARE SUB CheckPalindrome(num)
CLS
REM Main program
PRINT "Enter a number to check if it's a palindrome: ";
INPUT num
CALL CheckPalindrome(num)
END
REM Sub-procedure to check if a number is a palindrome
SUB CheckPalindrome (num)
original = num
reversed = 0
DO WHILE num <> 0
digit = num MOD 10
reversed = reversed * 10 + digit
num = num \ 10
LOOP
IF original = reversed THEN
PRINT original; " is a palindrome."
ELSE
PRINT original; " is not a palindrome."
END IF
END SUB
3. Program Using Two Sub-Procedures
Problem Statement:
Write a program with two sub-procedures: one to calculate the factorial of a number, and another to check if the number is even or odd.
Program:
DECLARE SUB CalculateFactorial(num)
DECLARE SUB CheckEvenOdd(num)
Main program
PRINT "Enter a number: ";
INPUT num
CALL CalculateFactorial(num)
CALL CheckEvenOdd(num)
END
REM Sub-procedure to calculate factorial
SUB CalculateFactorial (num)
factorial = 1
FOR i = 1 TO num
factorial = factorial * i
NEXT i
PRINT "The factorial of "; num; " is: "; factorial
END SUB
REM Sub-procedure to check if the number is even or odd
SUB CheckEvenOdd (num)
IF num MOD 2 = 0 THEN
PRINT num; " is even."
ELSE
PRINT num; " is odd."
END IF
END SUB
Explanation of Modular Programs
-
Sub-Procedures:
Each sub-procedure performs a specific task, promoting code reuse and better readability. -
Parameters:
- Parameters (
radius
,num
) are passed to sub-procedures for computation. This ensures the sub-procedures are modular and reusable.
- Parameters (
-
Two Sub-Procedures in One Program:
- The third program demonstrates the use of multiple sub-procedures in a single program to handle different tasks.
These programs exemplify modular programming principles and make the code easier to manage and debug. Let me know if you need further clarifications!
No comments:
Post a Comment