C program to find area of Rectangle |traingle

In this article, we are going to learn about how to find the area of a triangle and rectangle. We will learn both these shapes area calculations using the C language program.

  • How to calculate area of Traingle in C language.
  • How to calculate area of Rectangle in C language.

How to calculate area of Triangle in C language


In this example, we are going to learn the area of triangle calculation using C. We will learn this with the help of multiple formulas. First, we will use a simple formula for the area of a triangle, which is

Formula

Area=(1/2) * base * height

Second, we will learn it using another formula, which is and,

s=(a+b+c)/2
Area=sqrt(s(s-a)(s-b)*(s-c))1/2

where a,b,c are the sides length of the triangle.s is the half of the perimeter of the triangle.

C Program to find Area of Right angle triangle


so let us begin with our code example.

#include<stdio.h>
int main()
{
     float b, h, area;
     printf("Please enter the base and height of Traingle: ");
     scanf("%f %f", &b, &h);

     area = (0.5 * b * h);

     printf("The Area of Triangle is = %.2f\n", area );

     return 0;
}

Output

Please enter the base and height of Traingle: 12 13
The Area of Triangle is = 78.00

C Program to find Area of any triangle with sides a,b,c


#include<stdio.h>
#include<math.h>
int main()
{
     float a, b, c, s, area;

     printf("Please enter the sides length of Traingle: ");
     scanf("%f %f %f",&a,&b,&c);

     s = (a+b+c)/2;
     area = sqrt( s*(s-a)*(s-b)*(s-c) );

     printf("The Area of Triangle is = %.2f\n", area );

     return 0;
}

Output

Please enter the sides length of Traingle: 12 10
15
The Area of Triangle is = 59.81

How to calculate area of Rectangle in C language


In this example, we are going to calculate the area of a rectangle. To calculate the area we need the length and breadth of the rectangle.Using these sides length we can use the formula to calculate the area as:

Area = length * width

Let us see this with code example below.

#include<stdio.h>
int main()
{
     float length, width, Area;

     printf("Please enter the length & width of Rectangle: ");
     scanf("%f %f",&length,&width);

     Area = length * width;

     printf("The Area of Rectangle is = %.3f \n",Area);

     return 0;
}

output

Please enter the length & width of Rectangle: 12 13
The Area of Rectangle is = 156.000