Introduction
In this lab, you will learn how to create a Rectangle Area Calculator using C programming language. The lab covers the following steps:
Declare a Function to Calculate Rectangle Area: You will learn how to declare a function that takes the height and width of a rectangle as input, calculates the area, and returns the result.
Prompt User for Rectangle Dimensions: You will learn how to use standard input functions in C to prompt the user to enter the height and width of the rectangle.
Call the Rectangle Area Function: You will learn how to call the previously declared function and pass the user-provided dimensions to calculate the area.
Display the Calculated Rectangle Area: You will learn how to display the calculated rectangle area to the user.
Compile and Run the C Program: You will learn how to compile and run the C program to see the final result.
Declare a Function to Calculate Rectangle Area
In this step, you will learn how to declare a function to calculate the area of a rectangle in C. A function is a block of code that performs a specific task and can be reused throughout your program.
- Open the WebIDE and navigate to the
~/projectdirectory. - Create a new file called
rectangle.c:
cd ~/project
touch rectangle.c
- Open the
rectangle.cfile in the WebIDE editor and add the following function declaration:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
This function, calculateRectangleArea, takes two integer parameters:
height: representing the height of the rectanglewidth: representing the width of the rectangle
The function multiplies the height and width to calculate the area and returns the result as an integer.
intreturn type indicates the function will return an integer value- Function name
calculateRectangleAreais descriptive and follows camelCase naming convention - Parameters
heightandwidthrepresent the dimensions of the rectangle areais calculated by multiplying height and widthreturn areasends the calculated area back to the calling function
Prompt User for Rectangle Dimensions
In this step, you will learn how to prompt the user to input the dimensions of a rectangle using standard input functions in C.
- Open the
rectangle.cfile in the WebIDE and add themain()function to interact with the user:
void main()
{
int height, width;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
}
- Let's break down the user input process:
printf()displays a prompt message to the userscanf()reads the integer input from the user&heightand&widthare memory address references where input will be stored
- Update the complete
rectangle.cfile to include both the function and main method:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
}
Compile and run the program to test the user input prompts.
gcc rectangle.c -o rectangle
./rectangle
Example Output:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
Call the Rectangle Area Function
In this step, you will learn how to call the previously defined calculateRectangleArea() function and pass the user-input dimensions to calculate the rectangle's area.
- Update the
main()function in therectangle.cfile to call the area calculation function:
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
}
- Let's break down the function call:
rectangleAreastores the result returned by the functioncalculateRectangleArea(height, width)passes the user-input values as arguments- The function calculates the area and returns the result
- Update the complete
rectangle.cfile to include the function call:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
}
Compile and run the program to test the function call.
gcc rectangle.c -o rectangle
./rectangle
Example Output:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
Display the Calculated Rectangle Area
In this step, you will learn how to display the calculated rectangle area using the printf() function in C.
- Update the
main()function in therectangle.cfile to print the calculated area:
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
printf("The area of the rectangle is: %d square units\n", rectangleArea);
}
- Let's break down the output statement:
printf()is used to display text and values%dis a format specifier for integer valuesrectangleAreais the variable containing the calculated areasquare unitsis added to provide context to the result
- Update the complete
rectangle.cfile with the output statement:
#include <stdio.h>
int calculateRectangleArea(int height, int width)
{
int area = height * width;
return area;
}
void main()
{
int height, width, rectangleArea;
printf("Enter the height of the rectangle: ");
scanf("%d", &height);
printf("Enter the width of the rectangle: ");
scanf("%d", &width);
rectangleArea = calculateRectangleArea(height, width);
printf("The area of the rectangle is: %d square units\n", rectangleArea);
}
Compile and run the program to test the output display.
gcc rectangle.c -o rectangle
./rectangle
Example Output:
Enter the height of the rectangle: 5
Enter the width of the rectangle: 10
The area of the rectangle is: 50 square units
Summary
In this lab, you learned how to declare a function to calculate the area of a rectangle in C. The function, calculateRectangleArea, takes two integer parameters - height and width - and multiplies them to calculate the area, returning the result as an integer. You also learned how to prompt the user to input the dimensions of the rectangle using the printf() and scanf() functions, which display a prompt message and read the integer input from the user, respectively.
Next, you will learn how to call the rectangle area function, display the calculated area, and compile and run the C program.



