Write a C program to read length and width from the input and compute the perimeter and area of the rectangle.
Problem Definition:
In this program, we will read the length and breadth of the rectangle. Next, we find the area and perimeter of a rectangle.
Source code of C program to read length width and compute Area Perimeter
#include<stdio.h> int main() { float length,width; float area,perimeter; printf("Enter the length of a Rectangle: "); scanf("%f",&length); printf("Enter the width of a Rectangle: "); scanf("%f",&width); area=length*width; //calculates area perimeter=2*(length+width); //calculates perimeter printf("\nArea of a rectangle is %f",area); printf("\nPerimeter of a rectangle is %f",perimeter); }
Output:
Case 1:
Enter the length of a Rectangle: 10
Enter the width of a Rectangle: 20
Area of a rectangle is 200.000000
Perimeter of a rectangle is 60.000000
Case 2:
Enter the length of a Rectangle: 20
Enter the width of a Rectangle: 10
Area of a rectangle is 60.000000
Perimeter of a rectangle is 200.000000
Program Explanation:
1. First, create 4 variables such as length, breadth, area, and perimeter of type float.
2. Next, ask the user to enter the values of length and breadth.
3. Calculate the area of a rectangle using formula – > area = length * width
4. Calculate the perimeter of a rectangle using formula – > perimeter= 2 * (length * width)
5. Finally, display area and rectangle using printf statement.
Summary:
This tutorial discusses how to write a program to read length and width from the input and compute the perimeter and area of the rectangle. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.