Create a Rectangle Area Calculator in C

CCBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/FunctionsGroup -.-> c/function_declaration("Function Declaration") c/FunctionsGroup -.-> c/function_parameters("Function Parameters") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-438247{{"Create a Rectangle Area Calculator in C"}} c/function_declaration -.-> lab-438247{{"Create a Rectangle Area Calculator in C"}} c/function_parameters -.-> lab-438247{{"Create a Rectangle Area Calculator in C"}} c/user_input -.-> lab-438247{{"Create a Rectangle Area Calculator in C"}} c/output -.-> lab-438247{{"Create a Rectangle Area Calculator in C"}} end

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.

  1. Open the WebIDE and navigate to the ~/project directory.
  2. Create a new file called rectangle.c:
cd ~/project
touch rectangle.c
  1. Open the rectangle.c file 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 rectangle
  • width: representing the width of the rectangle

The function multiplies the height and width to calculate the area and returns the result as an integer.

  • int return type indicates the function will return an integer value
  • Function name calculateRectangleArea is descriptive and follows camelCase naming convention
  • Parameters height and width represent the dimensions of the rectangle
  • area is calculated by multiplying height and width
  • return area sends 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.

  1. Open the rectangle.c file in the WebIDE and add the main() 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);
}
  1. Let's break down the user input process:
  • printf() displays a prompt message to the user
  • scanf() reads the integer input from the user
  • &height and &width are memory address references where input will be stored
  1. Update the complete rectangle.c file 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.

  1. Update the main() function in the rectangle.c file 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);
}
  1. Let's break down the function call:
  • rectangleArea stores the result returned by the function
  • calculateRectangleArea(height, width) passes the user-input values as arguments
  • The function calculates the area and returns the result
  1. Update the complete rectangle.c file 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.

  1. Update the main() function in the rectangle.c file 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);
}
  1. Let's break down the output statement:
  • printf() is used to display text and values
  • %d is a format specifier for integer values
  • rectangleArea is the variable containing the calculated area
  • square units is added to provide context to the result
  1. Update the complete rectangle.c file 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.