C program to find perimeter of a rectangle

Write a C program to input length and width of a rectangle and calculate perimeter of the rectangle. How to find perimeter of a rectangle in C programming. Logic to find the perimeter of a rectangle if length and width are given in C programming.


Enter length: 5
Enter width: 10


Perimeter of rectangle = 30 units

Required knowledge

Operators, Data types, Defining Variables, Basic input/output

Perimeter of rectangle

Perimeter of rectangle is given by the below formula

Perimeter of rectangle
Where l is length and w is the width of rectangle.

Logic to find perimeter of a rectangle

Below is the step by step descriptive logic to find perimeter of a rectangle

  1. Input length and width of the rectangle using scanf() function. Store it in two variables say length and width.
  2. Calculate perimeter using formula for perimeter of rectangle perimeter = 2 * (length + width).
  3. Print the value of perimeter.

Program to find perimeter of rectangle

 * C program to find perimeter of rectangle

#include <stdio.h>

int main()
    float length, width, perimeter;

     * Input length and width of rectangle from user
    printf("Enter length of the rectangle: ");
    scanf("%f", &length);
    printf("Enter width of the rectangle: ");
    scanf("%f", &width);

    /* Calculate perimeter of rectangle */
    perimeter = 2 * (length + width);

    /* Print perimeter of rectangle */
    printf("Perimeter of rectangle = %f units ", perimeter);

    return 0;

Note: Never forget to prioritize the order of operations using a pair of braces ( ). Since, statements perimeter = 2 * length + width and perimeter = 2 * (length + width) will generate different results.

Read more - Precedence and associativity of operators

In addition, never write statement like 2 * (length + width) as 2(length + width). It will generate a compilation error.


Enter length of the rectangle: 5
Enter width of the rectangle: 10
Perimeter of rectangle = 30.000000

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>