C program to find perimeter of a rectangle

Write a C program to input length and width of a rectangle from user and calculate perimeter of the rectangle. How to find perimeter of a rectangle if length and breadth of a rectangle are given using C programming.

Example:
Input length: 5
Input Width: 10
Perimeter of rectangle = 30 units

Required knowledge:

Fundamentals of C, Data types, Taking user input in C, Perimeter of rectangle

Perimeter of rectangle:

Perimeter of a rectangle can be expressed using below formula:
Perimeter of rectangle
Where l is the length and w is the width of rectangle.

Program:

/**
 * C program to find perimeter of a rectangle
 */

#include <stdio.h>

int main()
{
    float length, width, perimeter;

    /*
     * Reads 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);

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

    /* Prints perimeter of rectangle */
    printf("\nPerimeter of rectangle = %f units ", perimeter);

    return 0;
} 


Output
X
_
Enter length of the rectangle: 5
Enter width of the rectangle: 10

Perimeter of rectangle = 30.000000

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment