Find the Area of a Parallelogram in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to calculate the area of a parallelogram using C programming. The lab covers the essential steps, including reading the base and height of the parallelogram, computing the area, and printing the result. By the end of this lab, you will have a solid understanding of geometry calculations in C and be able to apply this knowledge to solve various geometric problems.

The lab guides you through the complete process, starting with prompting the user to input the base and height of the parallelogram, then calculating the area using the formula "Area = Base * Height", and finally printing the computed area. This hands-on experience will help you develop your programming skills and deepen your understanding of geometric concepts.


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/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435180{{"`Find the Area of a Parallelogram in C`"}} c/variables -.-> lab-435180{{"`Find the Area of a Parallelogram in C`"}} c/user_input -.-> lab-435180{{"`Find the Area of a Parallelogram in C`"}} c/math_functions -.-> lab-435180{{"`Find the Area of a Parallelogram in C`"}} end

Read Base and Height

In this step, you will learn how to read the base and height of a parallelogram using C programming. This is the first crucial step in calculating the area of a parallelogram.

First, let's create a new C source file for our program:

cd ~/project
nano parallelogram_area.c

Now, add the following code to read base and height:

#include <stdio.h>

int main() {
    float base, height;

    printf("Enter the base of the parallelogram: ");
    scanf("%f", &base);

    printf("Enter the height of the parallelogram: ");
    scanf("%f", &height);

    printf("Base: %.2f\n", base);
    printf("Height: %.2f\n", height);

    return 0;
}

Example output:

Enter the base of the parallelogram: 5.5
Enter the height of the parallelogram: 3.2
Base: 5.50
Height: 3.20

Let's break down the code:

  • We use float to allow decimal values for base and height
  • printf() is used to prompt the user for input
  • scanf() reads the floating-point values entered by the user
  • We print the entered values to confirm correct input

Compile and run the program:

gcc parallelogram_area.c -o parallelogram_area
./parallelogram_area

Compute Area = Base * Height

In this step, you will learn how to calculate the area of a parallelogram by multiplying its base and height. We'll modify the previous program to include the area calculation.

Open the existing source file:

cd ~/project
nano parallelogram_area.c

Update the code to compute the area:

#include <stdio.h>

int main() {
    float base, height, area;

    printf("Enter the base of the parallelogram: ");
    scanf("%f", &base);

    printf("Enter the height of the parallelogram: ");
    scanf("%f", &height);

    // Compute area of parallelogram
    area = base * height;

    printf("Base: %.2f\n", base);
    printf("Height: %.2f\n", height);
    printf("Area of Parallelogram: %.2f\n", area);

    return 0;
}

Compile and run the updated program:

gcc parallelogram_area.c -o parallelogram_area
./parallelogram_area

Example output:

Enter the base of the parallelogram: 5.5
Enter the height of the parallelogram: 3.2
Base: 5.50
Height: 3.20
Area of Parallelogram: 17.60

Key points about area calculation:

  • Area of a parallelogram is calculated by multiplying base and height
  • We use a simple multiplication operation: area = base * height
  • The result is stored in the area variable
  • We print the area with two decimal places using %.2f format specifier

Print the Area

In this final step, we'll refine our parallelogram area calculation program by adding input validation and improving the output presentation.

Open the source file:

cd ~/project
nano parallelogram_area.c

Update the code with input validation and formatted output:

#include <stdio.h>

int main() {
    float base, height, area;

    // Input validation
    do {
        printf("Enter the base of the parallelogram (positive number): ");
        scanf("%f", &base);
    } while (base <= 0);

    do {
        printf("Enter the height of the parallelogram (positive number): ");
        scanf("%f", &height);
    } while (height <= 0);

    // Compute area of parallelogram
    area = base * height;

    // Formatted output
    printf("\n--- Parallelogram Area Calculation ---\n");
    printf("Base:   %.2f\n", base);
    printf("Height: %.2f\n", height);
    printf("Area:   %.2f square units\n", area);
    printf("------------------------------------\n");

    return 0;
}

Compile and run the final program:

gcc parallelogram_area.c -o parallelogram_area
./parallelogram_area

Example output:

Enter the base of the parallelogram (positive number): 5.5
Enter the height of the parallelogram (positive number): 3.2

--- Parallelogram Area Calculation ---
Base:   5.50
Height: 3.20
Area:   17.60 square units
------------------------------------

Key improvements:

  • Added input validation to ensure positive numbers
  • Created a formatted output with a title and clear layout
  • Added "square units" to clarify the area measurement
  • Used consistent formatting for numbers

Summary

In this lab, you learned how to read the base and height of a parallelogram using C programming, and then calculate its area by multiplying the base and height. The program prompts the user to enter the base and height, stores them in float variables, and then computes the area by multiplying the two values. Finally, it prints the base, height, and the calculated area of the parallelogram.

Other C Tutorials you may like