Home >Backend Development >C++ >C program for area and perimeter of rectangle

C program for area and perimeter of rectangle

PHPz
PHPzforward
2023-09-20 10:41:011783browse

Given the length and width of a rectangle, we need to find its area and perimeter.

A rectangle is a two-dimensional shape with four sides and four corners, each of which is 90 degrees. All sides of a rectangle are not equal, only the opposite sides of the rectangle are equal. The diagonals of a rectangle also have the same length.

The following is a schematic diagram of a rectangle.

C program for area and perimeter of rectangle

Here A represents the width of the rectangle, and B represents the length of the rectangle.

To find the area of a rectangle, the formula is: length x width

The perimeter of a rectangle is 2 x (length width) .

Example

Input: 20 30
Output: area of rectangle is : 600
   perimeter of rectangle is : 100

Algorithm

START
   In Function int area(int a, int b)
   Step 1 -> Declare an integer ‘area’ and store a * b in it
   Step 2 -> Return area.
   In Function int perimeter(int a, int b)
   Step 1-> Declare an integer ‘perimeter’ and store 2*(a + b) in it
   Step 2-> Return perimeter
   In int main()
   Step 1 -> Declare two integers ‘length’, ‘breadth’
   Step 2 -> Print area(length,breadth)
   Step 3 -> Print perimeter(length, breadth);
STOP

Example

#include<stdio.h>
//function to calculate area
int area(int a, int b) {
   int area = a * b;
   return area;
}
//function to calculate perimeter
int perimeter(int a, int b){
   int perimeter = 2*(a + b);
   return perimeter;
}
int main(){
   int length= 20;
   int breadth = 30;
   printf("area of rectangle is : %d</p><p>",area(length,breadth));
   printf("perimeter of rectangle is : %d",perimeter(length, breadth));
   return 0;
}

Output

area of rectangle is : 600
perimeter of rectangle is : 100

The above is the detailed content of C program for area and perimeter of rectangle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete