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

C program for area and perimeter of rectangle

PHPz
Release: 2023-09-20 10:41:01
forward
1785 people have browsed it

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
Copy after login

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
Copy after login

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;
}
Copy after login

Output

area of rectangle is : 600
perimeter of rectangle is : 100
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template