Area of a Rectangle Circle and Trapezium in C
In this C programming algorithm tutorial we are looking at how to implement the mathematical formulas to determine area of a rectangle, circle and trapezium. We will give you the formula, the C source code and the results.
Area of a Rectangle
The mathematical formula to determine the area of a rectangle is:
area = length * width
Let’s look at the implementation in C language of this formula:
#include <stdio.h>
int main() {
float length,width;
float area;
printf("Enter the length and width of the rectangle sides: ");
scanf("%f%f",&length, &width);
area = length * width;
printf("Area of rectangle is: %.2f\n", area);
return 0;
}
Example output of the source code will look something like this:
Enter the length and width of the rectangle sides: 2 2
Area of rectangle is: 4.00
Area of a Circle
The mathematical formula to determine the area of a circle is:
area = pi*radius*radius
As you can see we are using the constant PI (π). We are only using 5 decimal digits (that’s enough accuracy for this formula). Of course PI is much larger, for instance here are the first 100 decimal digits of π = 3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
printf("Input the radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("%f\n", area);
return 0;
}
Output example of the source code:
Input the radius: 12
452.389343
Area of a Trapezium
The mathematical formula to determine the area of a trapezium is:
area = (1/2) * (a + b) * h
Take a look at the implementation of this formula in C language:
#include <stdio.h>
int main() {
float base1,base2,height;
float area;
printf("Enter the size of two bases and height of the trapezium: ");
scanf("%f%f%f",&base1,&base2,&height);
area = 0.5 * ( base1 + base2 ) * height ;
printf("Area of trapezium is: %.3f\n",area);
return 0;
}
Output example of the source code:
Enter the size of two bases and height of the trapezium: 2 2 3
Area of trapezium is: 6.000
As you can see, these formulas are easy to implement. Also take a look at how to implement formulas to determine the area of different triangles.
That’s all for this tutorial.
Wonderful programming solutions.
Am greatly assisted. Thank you
Am greatly thankful to the programmer.I’ve been assisted by the program post and I now strongly believe that I tackle several problems