Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

# How to Compute the Perimeter and Area of a Rectangle in C

program that can calculate the perimeter and area of a rectangle. code to to compute the perimeter and area of a rectangle with a heigh 7 inches 5 inc

If you are learning C programming, you might be wondering how to write a simple program that can calculate the perimeter and area of a rectangle. In this program, I will show you how to do that in just a few lines of code.

First, let's review some basic concepts. A rectangle is a quadrilateral with four right angles and two pairs of parallel sides. The perimeter of a rectangle is the sum of the lengths of all its sides, and the area of a rectangle is the product of the lengths of its base and height.

#### C Code

To write a C program that can compute the perimeter and area of a rectangle, we need to do the following steps:

• Declare variables to store the height, width, perimeter and area of the rectangle.
• Assign values to the height and width variables. In this example, we will use 7 inches for height and 5 inches for width, but you can change them as you wish.
• Compute the perimeter and area using the formulas: perimeter = 2 * (height + width) and area = height * width.
• Print the results using the printf function.

Here is the code to to compute the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches:

#include <stdio.h>

int main()

{

// Declare variables

int height = 7;

int width = 5;

int perimeter;

int area;

// Compute perimeter and area

perimeter = 2 * (height + width);

area = height * width;

// Print results

printf("The perimeter of the rectangle is %d inches.\n", perimeter);

printf("The area of the rectangle is %d square inches.\n", area);

return 0;

}

#### Explanation

Let's go through the code line by line and see how it works.

• The first line '#include <stdio.h>' is a preprocessor directive that tells the compiler to include the standard input/output header file. This file contains the declaration of the printf function that we will use later.
• The second line 'int main()' is the main function of the program. Every C program must have one and only one main function. This is where the execution of the program starts and ends.
• The next four lines are comments that start with '//'. Comments are used to explain or document the code for yourself or others. They are ignored by the compiler and do not affect the output of the program.
• The next four lines declare four variables: height, width, perimeter and area. Variables are names that represent data or values in memory. They have a type (in this case int for integer) and a name (such as height or width). We can assign values to variables using the '=' operator.
• The next two lines compute the perimeter and area using arithmetic operators such as '*' for multiplication and '+' for addition. We can use parentheses to group expressions and control their order of evaluation.
• The last two lines print the results using the printf function. The printf function takes a format string as its first argument and any number of additional arguments that match the placeholders in the format string. For example, '%d' is a placeholder for an integer value. The '\n' character represents a newline that moves the cursor to the next line.

#### Output

If you run this program, you will get the following output:

The perimeter of the rectangle is 24 inches.

The area of the rectangle is 35 square inches.

As you can see, writing a C program to compute the perimeter and area of a rectangle is not difficult at all. You just need to know how to declare variables, assign values, use arithmetic operators and print statements.

#### Conclusion

I hope this program was helpful for you. If you want to learn more about C programming, you can check out some of my other posts on this topic. Thank you for reading and happy coding!

We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!