C Tutorial

# C Program to find Area of a Triangle

## Area of a triangle formula

Area of a triangle =  (base * height)/2

Area of a triangle = square root of (s(s-a)(s-b)(s-c)) here a,b and c are sides of the triangle. s = (a+b+c)/2

Area of an equilateral triangle = (square root of (3) * side * side)/4

## C Program to find the area of a triangle using base and height

This C program gets base and height as user inputs and computes the area of a triangle.

#include<stdio.h>

void main()
{
int base, height, area=0;
printf("\nFinds area of triangle using base and height");
printf("\n-------------------------------------------");
printf("\nEnter base: ");
scanf("%d", &base);
printf("Enter height: ");
scanf("%d", &height);

area = (base * height) / 2;

printf("Area of triangle: %d", area);
}

Output:
\$ cc area-of-triangle.c
\$ ./a.out

Finds area of triangle using base and height
-------------------------------------------
Enter base: 5
Enter height: 6
Area of triangle: 15

## C Program to find the area of a triangle using triangle sides

This C program gets all the triangle sides as user inputs and computes the area of a triangle.

#include<stdio.h>
#include<math.h>

void main()
{
int a, b, c, s;
double area=0;
printf("\nFinds area of triangle using triangle sides");
printf("\n-------------------------------------------");
printf("\nEnter side a: ");
scanf("%d", &a);
printf("Enter side b: ");
scanf("%d", &b);
printf("Enter side c: ");
scanf("%d", &c);

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

printf("Area of triangle: %.2lf", area);
}

Output:
\$ cc area-of-triangle.c -o a.out -lm
\$ ./a.out

Finds area of triangle using triangle sides
-------------------------------------------
Enter side a: 6
Enter side b: 7
Enter side c: 8
Area of triangle: 15.49[

## C Program to find the area of an equilateral triangle using triangle side

This C program gets all the triangle side as user input and computes the area of an equilateral triangle.

#include<stdio.h>
#include<math.h>

void main()
{
int side;
double area=0;
printf("\nFinds area of equilateral triangle using triangle side");
printf("\n-------------------------------------------");
printf("\nEnter side: ");
scanf("%d", &side);

area = (double)((sqrt(3)*side*side)/4);

printf("Area of equilateral triangle: %.2lf", area);
}

Output:
\$ cc area-of-triangle.c -o a.out -lm
\$ ./a.out

Finds area of equilateral triangle using triangle side
-------------------------------------------
Enter side: 8
Area of equilateral triangle: 27.71

C Tutorial