Home  >  Article  >  Backend Development  >  How to calculate the perimeter and area of ​​a rectangle in C language? (with code)

How to calculate the perimeter and area of ​​a rectangle in C language? (with code)

藏色散人
藏色散人Original
2018-11-06 17:50:1929494browse

This article mainly introduces you to the C program to calculate the perimeter and area of ​​a rectangle.

For example, here is a rectangle with a height of 7 inches and a width of 5 inches. How do we calculate its perimeter and area in C language.

Below we will introduce the solution in detail through specific code examples.

1.C Programming: The perimeter of a rectangle

The perimeter is the path around the two-dimensional shape. The word comes from the Greek peri (around) and meter (measure). Perimeter can be used to calculate the length of fence needed to surround your yard or garden. For a rectangle with only two different sides, say x and y, the perimeter is equal to 2x 2y.

2.C Programming: Rectangular Area

The area of ​​a two-dimensional graphic describes the amount of surface covered by the shape. You can measure area in fixed-sized square units, such as square inches, square centimeters, or square miles. The formula for a rectangular area uses multiplication: length·width=area. A rectangle with four sides of equal length is a square.

cThe code for calculating the perimeter and area is as follows:

#include <stdio.h> 
/* height and width of a rectangle in inches */
int width;          
int height;         

int area;           
int perimeter;      

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

    perimeter = 2*(height + width);
    printf("Perimeter of the rectangle = %d inches\n", perimeter);
    
    area = height * width;
    printf("Area of the rectangle = %d square inches\n", area);

return(0);
}

The calculation result is displayed as:

Perimeter of the rectangle = 24 inches                                 
Area of the rectangle = 35 square inches

which is the perimeter of the rectangle = 24 inches, area = 35 square inches.

The calculation process can also be shown as follows:

How to calculate the perimeter and area of ​​a rectangle in C language? (with code)

This article is about how to calculate the perimeter and area of ​​a rectangle in C language The introduction 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 perimeter and area of ​​a rectangle in C language? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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