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: 5 
Perimeter of rectangle = 20 units

Required knowledge:

Fundamentals of CData typesTaking user input in CPerimeter 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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * C program to find perimeter of a rectangle
 */
#include <stdio.h>

void 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);
}


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

Perimeter of rectangle = 20.000000

Enjoy it...............

Comments

Popular posts from this blog

C program to calculate velocity

C program to find square root of a number

C program to find maximum between two numbers