Calculate the Area of a Rectangle in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to calculate the area of a rectangle using C programming. The lab consists of three main steps: reading the length and width of the rectangle, computing the area by multiplying the length and width, and printing the calculated area. The program will prompt the user to enter the length and width, and then display the calculated area. This lab provides a practical introduction to geometry calculations using C and demonstrates how to perform basic mathematical operations in a C program.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435135{{"`Calculate the Area of a Rectangle in C`"}} c/variables -.-> lab-435135{{"`Calculate the Area of a Rectangle in C`"}} c/operators -.-> lab-435135{{"`Calculate the Area of a Rectangle in C`"}} c/user_input -.-> lab-435135{{"`Calculate the Area of a Rectangle in C`"}} c/math_functions -.-> lab-435135{{"`Calculate the Area of a Rectangle in C`"}} end

Read Length and Width

In this step, you will learn how to read the length and width of a rectangle using C programming. We'll create a simple program that allows user input for the rectangle's dimensions.

First, let's create a new C file in the project directory:

cd ~/project
nano rectangle_area.c

Now, enter the following code to read length and width:

#include <stdio.h>

int main() {
    float length, width;

    // Prompt user to enter length
    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the rectangle: ");
    scanf("%f", &width);

    // Print the input dimensions
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);

    return 0;
}

Let's compile and run the program:

gcc rectangle_area.c -o rectangle_area
./rectangle_area

Example output:

Enter the length of the rectangle: 5.5
Enter the width of the rectangle: 3.2
Length: 5.50
Width: 3.20

Code Explanation:

  • #include <stdio.h> includes the standard input/output library
  • float length, width; declares two float variables to store decimal numbers
  • printf() is used to display prompts to the user
  • scanf() reads user input and stores it in the specified variables
  • %.2f formats the output to display two decimal places

Compute Area = Length * Width

In this step, you will modify the previous program to calculate the area of a rectangle by multiplying its length and width.

Open the existing C file:

cd ~/project
nano rectangle_area.c

Update the code to compute and display the area:

#include <stdio.h>

int main() {
    float length, width, area;

    // Prompt user to enter length
    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the rectangle: ");
    scanf("%f", &width);

    // Calculate the area
    area = length * width;

    // Print the input dimensions and calculated area
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);
    printf("Area: %.2f\n", area);

    return 0;
}

Compile and run the updated program:

gcc rectangle_area.c -o rectangle_area
./rectangle_area

Example output:

Enter the length of the rectangle: 5.5
Enter the width of the rectangle: 3.2
Length: 5.50
Width: 3.20
Area: 17.60

Code Explanation:

  • Added a new variable area to store the calculation result
  • Used the multiplication operator * to compute area
  • area = length * width calculates the rectangle's area
  • Added a new printf() to display the calculated area

Print the Area

In this step, you will enhance the program to provide a more descriptive output of the rectangle's area calculation.

Open the existing C file:

cd ~/project
nano rectangle_area.c

Update the code to format the area output more professionally:

#include <stdio.h>

int main() {
    float length, width, area;

    // Prompt user to enter length
    printf("Rectangle Area Calculator\n");
    printf("------------------------\n");
    printf("Enter the length of the rectangle: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the rectangle: ");
    scanf("%f", &width);

    // Calculate the area
    area = length * width;

    // Print formatted area result
    printf("\nCalculation Results:\n");
    printf("Length: %.2f units\n", length);
    printf("Width: %.2f units\n", width);
    printf("Area: %.2f square units\n", area);

    return 0;
}

Compile and run the updated program:

gcc rectangle_area.c -o rectangle_area
./rectangle_area

Example output:

Rectangle Area Calculator
------------------------
Enter the length of the rectangle: 6.0
Enter the width of the rectangle: 4.5

Calculation Results:
Length: 6.00 units
Width: 4.50 units
Area: 27.00 square units

Code Explanation:

  • Added descriptive headers and formatting
  • Included units in the output for clarity
  • Improved readability of the calculation results
  • Used \n for line breaks to create visual separation

Summary

In this lab, you will learn how to read the length and width of a rectangle, compute the area by multiplying the dimensions, and print the result. First, you will prompt the user to enter the length and width of the rectangle, and store the values in variables. Then, you will calculate the area by multiplying the length and width, and display the result to the user.

The key learning points are: using the printf() function to display prompts, utilizing the scanf() function to read user input, and performing arithmetic calculations to compute the area of the rectangle.

Other C Tutorials you may like