Home >Backend Development >C#.Net Tutorial >How to calculate the area and perimeter of a rectangle in C language
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.
Perimeter of a rectangle:
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:
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); }
The calculation result is as follows:
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!