codescracker
c

C Program to Find Area and Perimeter of Rectangle



« Previous Program Next Program »

In this article, you will learn and get code about calculating area and perimeter of rectangle. Here are the list of program described in this article:

How to Calculate Area of Rectangle ?

To calculate area of any rectangle, use the formula area = len*bre. Here len is the length and bre is the breadth of rectangle.

Area of Rectangle Program in C

The question is, Write a program in C to calculate area of rectangle. The length and breadth of Rectangle must be defined by user. The answer of this question is given below:

// Calculate Area of Rectangle Program in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    float len, bre, area;
    printf("Enter Length of Rectangle: ");
    scanf("%f", &len);
    printf("Enter breadth of Rectangle: ");
    scanf("%f", &bre);
    area = len*bre;
    printf("\nArea = %0.2f", area);
    getch();
    return 0;
}

This program is compiled and executed under Code::Blocks IDE. Here is the first snapshot of sample run produced by above program:

c find area of rectangle

Now supply any input as length of rectangle say 2.5 and press ENTER key. Again supply the breadth of rectangle say 3 and press ENTER key to see the output as shown in the snapshot given below:

area of rectangle program c

Steps involved in previous Program

Here are the list of some main steps used in previous program:

Area of Rectangle Program using Function in C

Let's create another program that does the same job as of previous program. The only difference is, previous program finds area of rectangle without using function, but this program uses user-defined function to find area of rectangle as given below:

// Area of Rectangle Program using Function in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
float findAreaFun(float, float);
int main()
{
    float len, bre, area;
    printf("Enter Length of Rectangle: ");
    scanf("%f", &len);
    printf("Enter breadth of Rectangle: ");
    scanf("%f", &bre);
    area = findAreaFun(len, bre);
    printf("\nArea = %0.2f", area);
    getch();
    return 0;
}
float findAreaFun(float l, float b)
{
    return l*b;
}

This program produces same output as of previous program.

How to Calculate Perimeter of Rectangle ?

To calculate perimeter of rectangle, use formula, perimeter = 2*(len+bre). Here len indicates to length and bre indicates to breadth of rectangle.

As perimeter is the length of boundary of rectangle. And there are four boundary of rectangle, in which the two adjacent boundary is equal to its opposite boundary. That is to calculate perimeter, we can also write the above formula as perimeter = len+len+bre+bre.

Perimeter of Rectangle Program in C

The question is write a program in c that prints perimeter of rectangle. Length and Breadth of rectangle must be defined by user.. The answer to this question is given below:

// Perimeter of Rectangle Program in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    float len, bre, perimeter;
    printf("Enter Length of Rectangle: ");
    scanf("%f", &len);
    printf("Enter Breadth of Rectangle: ");
    scanf("%f", &bre);
    perimeter = 2*(len+bre);
    printf("\nPerimeter = %0.2f", perimeter);
    getch();
    return 0;
}

Here is the sample run of above program:

perimeter of rectangle c

Now supply any two input as length and bredth of rectangle one by one, say 2.5 (as length) and 3.5 (as bread). Press ENTER key to see the output as shown in the snapshot given below:

c perimeter of rectangle program

Perimeter of Rectangle in C using Function

Here is the last program of this article, this program does the same job as of previous program. The only difference in coding point of view to this program is, it is created using function to calculate the perimeter of rectangle. Let's take a look at here:

// Perimeter of Rectangle Program in C using Function
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
float findPerimeterFun(float, float);
int main()
{
    float len, bre, perimeter;
    printf("Enter Length of Rectangle: ");
    scanf("%f", &len);
    printf("Enter Breadth of Rectangle: ");
    scanf("%f", &bre);
    perimeter = findPerimeterFun(len, bre);
    printf("\nPerimeter = %0.2f", perimeter);
    getch();
    return 0;
}
float findPerimeterFun(float l, float b)
{
    return 2*(l+b);
}

The output produced by above program is totally same as of previous program. To learn more about function, refer to Function in C tutorial.


« Previous Program Next Program »