How to calculate the area and perimeter of a rectangle in C language

藏色散人
Release: 2018-12-11 09:16:18
Original
33150 people have browsed it



Calculating the area and perimeter of a rectangle in C language is one of the common questions during the interview process. In fact, it is very simple. The formula for calculating the area and perimeter of a rectangle is that the perimeter is equal to 2x 2y, and the area of ​​the rectangle = length x width.

How to calculate the area and perimeter of a rectangle in C language

Perimeter of a rectangle:

How to calculate the area and perimeter of a rectangle in C language

The perimeter is the path around the two-dimensional shape. Perimeter can be used to calculate the length of fence needed to enclose a yard or garden. For a rectangle with only two sides, say x and y, the perimeter is equal to 2x 2y.

Area of ​​a rectangle:

How to calculate the area and perimeter of a rectangle in C language

The formula for the area of ​​a rectangle uses multiplication: length x width = area. A rectangle with four equal sides is a square. The units of rectangular area are square meters, square centimeters, etc.

c language calculates the perimeter and area of ​​a rectangle

The code example is as follows:

#include <stdio.h> 
/* 长方形的高和宽,单位为米*/
int width;          
int height;         

int area;           
int perimeter;      

int main() {
	height = 7;
	width = 5;

    perimeter = 2*(height + width);
	printf("矩形的周长 = %d 米\n", perimeter);
	
	area = height * width;
	printf("矩形的面积 = %d 平方米\n", area);

return(0);
}
Copy after login

The calculation result is as follows:

How to calculate the area and perimeter of a rectangle in C language

This article is about how to calculate the area and perimeter of a rectangle in C language. It is very simple and easy to understand. I hope it will be helpful to friends in need!



The above is the detailed content of How to calculate the area and perimeter of a rectangle in C language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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