C program to find the area and perimeter of a rectangle

In high school mathematics, the most common formulas of a rectangle are perimeter, area, diagonal, etc. In this tutorial, we will learn how to implement those formulae in the C program and make a console application that will ask for the base and height of a rectangle and calculates the area and perimeter.

Algorithm – FInd the Area & Perimeter

Our entry point of this program is the main function. In the main function, we will declare the necessary variables to store base, height, perimeter, and area as float type.

Then we will print the message for taking corresponding input using the printf method. And then scanf function for taking the base and height input.

Then we will calculate using the corresponding formula and keep the result in the corresponding variables. Finally print the result using the printf function.

C program to find the area and perimeter of a rectangle

#include <stdio.h>
int main() {
float base, height, perimeter, area;
printf("Enter base: ");
scanf("%f", &base);
printf("Enter height: ");
scanf("%f", &height);

perimeter = 2*(base+height);
area = base*height;

printf("Perimeter of the rectangle is: %.2f ", perimeter);
printf("and area is %.2f\n", area);

return 0;
}
Sample Input Sample Output
Enter base: 5.5
Enter height: 3.4
Perimeter of the rectangle is: 17.80 and area is 18.70
Posted in