Here’s the lab program structured step by step, with the questions first, followed by the answers:
Question 1: Calculate Area and Volume of a Rectangle
Write a QBASIC program that calculates the area and volume of a rectangle. Use a SUB procedure to call FUNCTIONS that calculate the area and volume.
Answer to Question 1:
REM Declarations for SUBs and FUNCTIONS
DECLARE SUB RectangleArea(length, width)
DECLARE FUNCTION RectangleVolume(length, width, height)
REM Main Program
PRINT "Enter length, width, and height of the rectangle: ";
INPUT length, width, height
REM Call the sub-procedure to calculate area and volume
CALL RectangleArea(length, width)
PRINT"Rectangle Volume";RectangleVolume(length,width,height)
END
REM Sub-procedure for Rectangle Calculations
SUB RectangleArea (length, width, height)
Area=length*width
PRINT "Rectangle Area: "; Area
END SUB
REM Function to calculate the volume of a rectangle
FUNCTION RectangleVolume (length, width, height)
RectangleVolume = length * width * height
END FUNCTION
Question 2: Calculate Area and Perimeter of a Circle
Write a QBASIC program that calculates the area and perimeter (circumference) of a circle. Use a SUB procedure to call FUNCTIONS that calculate the area and perimeter.
Answer to Question 2:
REM Declarations for SUBs and FUNCTIONS
DECLARE SUB CircleArea(radius)
DECLARE FUNCTION CirclePerimeter(radius)
CLS
REM Main Program
REM Get input from the user for the circle radius
PRINT "Enter the radius of the circle: ";
INPUT radius
REM Call the sub-procedure to calculate area and perimeter
CALL CircleArea(radius)
PRINT"CirclePerimeter: ";CirclePerimetrer(radius)
END
REM SUB to calculate the area of a circle
SUB CircleArea (radius)
Area = 3.14159 * radius * radius
PRINT "Circle Area: "; Area
END SUB
REM Function to calculate the perimeter (circumference) of a circle
FUNCTION CirclePerimeter (radius)
CirclePerimeter = 2 * 3.14159 * radius
END FUNCTION
Question 3: Calculate Area and Volume of a Cube
Write a QBASIC program that calculates the surface area and volume of a cube. Use a SUB procedure to call FUNCTIONS that calculate the area and volume.
Answer to Question 3:
REM Declarations for SUBs and FUNCTIONS DECLARE SUB CubeArea(side) DECLARE FUNCTION CubeVolume(side) CLS
REM Main Program
REM Get input from the user for the cube side length
PRINT "Enter the side length of the cube: ";
INPUT side
REM Call the sub-procedure to calculate area and volume
CALL CubeArea(side)
PRINT"CubeVoulme";CubeVolume(side)
END
REM Sub-procedure for Cube Calculations
SUB CubeArea (side)
Area = 6 * side * side
PRINT "Cube Surface Area: "; Area
END SUB
REM Function to calculate the volume of a cube
FUNCTION CubeVolume (side)
CubeVolume = side * side * side
END FUNCTION
Sample Outputs
For Question 1: Rectangle Calculations
Input:
Enter length, width, and height of the rectangle: 5, 3, 4
Output:
Rectangle Area: 15
Rectangle Volume: 60
For Question 2: Circle Calculations
Input:
Enter the radius of the circle: 7
Output:
Circle Area: 153.93804
Circle Perimeter (Circumference): 43.98226
For Question 3: Cube Calculations
Input:
Enter the side length of the cube: 4
Output:
Cube Surface Area: 96
Cube Volume: 64
Explanation of the Program Structure
-
Question 1: Rectangle Calculations
- SUB Procedure (
RectangleCalculations
) calls two FUNCTIONS (RectangleArea
andRectangleVolume
) to compute the area and volume of the rectangle.
- SUB Procedure (
-
Question 2: Circle Calculations
- SUB Procedure (
CircleCalculations
) calls two FUNCTIONS (CircleArea
andCirclePerimeter
) to compute the area and perimeter of the circle.
- SUB Procedure (
-
Question 3: Cube Calculations
- SUB Procedure (
CubeCalculations
) calls two FUNCTIONS (CubeArea
andCubeVolume
) to compute the surface area and volume of the cube.
- SUB Procedure (
Each question demonstrates how to use SUB procedures for modularity and FUNCTIONS for specific calculations in a structured and reusable way. Let me know if you need further clarifications!
No comments:
Post a Comment