## C program to find the perimeter of a rectangle:

In this post, we will learn how to find the *perimeter* of a *rectangle* in *C programming*. The program will take the values as inputs from the user to calculate the perimeter and it will print the value. With this program, you will learn how to take user input values, how to do simple mathematical calculations, and how to print values on the console in C.

### Algorithm to find the perimeter of a rectangle:

To find the *perimeter* of a rectangle, we need the *length* and *width* of the rectangle. We can use the below formula to find the perimeter:

`2 * (length + width)`

So, if we can get the *length* and *width* values as inputs from the user, we can calculate its perimeter.

### Example 1: C program to find the perimeter of a rectangle:

Let’s write down the *C program* to find the perimeter of a rectangle with user input length and width:

```
#include <stdio.h>
int main()
{
float length, width, perimeter;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
perimeter = 2 * (length + width);
printf("Perimeter: %.2f\n", perimeter);
}
```

Here,

- It created three float variables
*length*,*width*and*perimeter*. - It asks the user to enter the length and width of the rectangle. It uses
*scanf*to read the user input numbers and assigns these to*length*and*width*variables. - The perimeter of the rectangle is calculated and assigned it to the
*perimeter*variable. - The last line is printing the value of the perimeter.

It will print output as below:

```
Enter the length of the rectangle: 12
Enter the width of the rectangle: 13
Perimeter: 50.00
```

### Example 2: C program to find the perimeter of a rectangle by using a separate function:

Let’s use a separate function to find the perimeter. We will create another function that will calculate the perimeter. It will be a separate function and we can call it from any other places we want. Let me show you the program:

```
#include <stdio.h>
float getPerimeter(float length, float width)
{
return 2 * (length + width);
}
int main()
{
float length, width, perimeter;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
printf("Perimeter: %.2f\n", getPerimeter(length, width));
}
```

Here,

- The
*getPerimeter*function is used to calculate the perimeter. It takes the length and the width of the rectangle as its parameters and returns the*perimeter*of the rectangle.

It will print similar output.

### You might also like:

- 3 different C programs to print odd numbers between 1 to 100
- 3 different C programs to print the grade of a student
- 3 different C programs to print an inverted Pyramid pattern
- C program to print the multiplication table using do…while loop
- 10 different alphabet pattern programs in C
- C program to find the distance between two cities in different units
- C program to subtract two user input numbers
- C program to print the current date-time in different formats