Area of Rectangle in C

Definition:

A rectangle is a four-sided shape with opposite sides equal. The area of a rectangle is calculated by multiplying its width by its length.

Formula:

Area = Width × Length

Explanation:

This program accepts the width and length of a rectangle from the user using the scanf() function. Both values are stored as float data type, and the area is calculated using the formula Area = Width × Length.


Program: Calculate Area of Rectangle

Explanation: This program accepts the width and length of a rectangle and calculates its area.


#include <stdio.h>
#include <conio.h>

main()
{
    float a,w,l;

    clrscr();

    printf("Enter Width of Rectangle : ");
    scanf("%f",&w);

    printf("Enter Length of Rectangle : ");
    scanf("%f",&l);

    a = w * l;

    printf("\nArea of Rectangle = %.2f",a);

    getch();
}

Output:

Enter Width of Rectangle : 5.5
Enter Length of Rectangle : 4.0

Area of Rectangle = 22.00

Program: Calculate Area of Square

Explanation: This program accepts the side of a square and calculates its area using the formula Side × Side.


#include <stdio.h>
#include <conio.h>

main()
{
    float side,area;

    clrscr();

    printf("Enter Side of Square : ");
    scanf("%f",&side);

    area = side * side;

    printf("\nArea of Square = %.2f",area);

    getch();
}

Output:

Enter Side of Square : 6

Area of Square = 36.00

Program: Calculate Area of Triangle

Explanation: This program accepts the base and height of a triangle and calculates its area using the formula (Base × Height) / 2.


#include <stdio.h>
#include <conio.h>

main()
{
    float base,height,area;

    clrscr();

    printf("Enter Base : ");
    scanf("%f",&base);

    printf("Enter Height : ");
    scanf("%f",&height);

    area = (base * height) / 2;

    printf("\nArea of Triangle = %.2f",area);

    getch();
}

Output:

Enter Base : 8
Enter Height : 5

Area of Triangle = 20.00

Practice Exercises

Task 1: Write a C program to calculate the area of a rectangle by accepting the width and length from the user.

Goal: Learn how to use the float data type and scanf() function to calculate the area of a rectangle.
💡 Hint: Use the formula Area = Width × Length.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    float base,height,area;

    clrscr();

    printf("Enter Base : ");
    scanf("%f",&base);

    printf("Enter Height : ");
    scanf("%f",&height);

    area = (base * height) / 2;

    printf("\nArea of Triangle = %.2f",area);

    getch();
}

Output
Enter Width : 8
Enter Length : 5

Area of Rectangle = 40.00
Explanation: The program accepts the width and length of a rectangle, calculates the area using Area = Width × Length, and displays the result.
Task 2: Write a C program to calculate the area of a square by accepting the side from the user.

Goal: Learn how to calculate the area of a square using the float data type.
💡 Hint: Use the formula Area = Side × Side.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    float side, area;

    clrscr();

    printf("Enter Side : ");
    scanf("%f",&side);

    area = side * side;

    printf("\nArea of Square = %.2f", area);

    getch();
}

Output
Enter Side : 6

Area of Square = 36.00
Explanation: The program accepts the side of a square, calculates the area using Area = Side × Side, and displays the result.
Task 3: Write a C program to calculate the area of a triangle by accepting the base and height from the user.

Goal: Learn how to calculate the area of a triangle using the float data type.
💡 Hint: Use the formula Area = (Base × Height) / 2.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    float base, height, area;

    clrscr();

    printf("Enter Base : ");
    scanf("%f",&base);

    printf("Enter Height : ");
    scanf("%f",&height);

    area = (base * height) / 2;

    printf("\nArea of Triangle = %.2f", area);

    getch();
}

Output
Enter Base : 10
Enter Height : 6

Area of Triangle = 30.00
Explanation: The program accepts the base and height of a triangle, calculates the area using Area = (Base × Height) / 2, and displays the result.