C program to find area of an equilateral triangle

Previous Program Next Program

Write a C program to input side of an equilateral triangle from user and find area of the given equilateral triangle. How to find area of an equilateral triangle if sides of the triangle are given in C programming. C program to calculate area of an equilateral triangle if its side is given.

Example

Input

Enter side of the equilateral triangle: 10

Output

Area of equilateral triangle = 43.3 sq. units

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Area of equilateral triangle

Area of equilateral triangle is given by the below formula -

Area of equialateral triangle

Logic to find area of equilateral triangle

The real logic of the program lies in converting the above mathematical formula in programming notation. For transforming the above mathematical formula to programming notation you must know two things -

The mathematical formula for area of equilateral triangle in programming notation can be written as (sqrt(3) / 4) * (side * side). Let us now use this formula to find area of equilateral triangle.

Below is the step by step descriptive logic to find area of an equilateral triangle -

  1. Input side of the equilateral triangle in some variable say side.
  2. Apply the formula to find area i.e. area = (sqrt(3) / 4) * (side * side).
  3. Finally print the value of resultant area.

Program to find area of an equilateral triangle

/**
 * C program to find area of an equilateral triangle
 */

#include <stdio.h>
#include <math.h> //Used for sqrt() function

int main()
{
    float side, area;

    // Input side of equilateral triangle
    printf("Enter side of an equilateral triangle: ");
    scanf("%f", &side);

    // Calculate area of equilateral triangle
    area = (sqrt(3) / 4) * (side * side);

    // Print the area
    printf("Area of equilateral triangle = %.2f sq. units", area);

    return 0;
} 
Output
Enter side of an equilateral triangle: 75
Area of equilateral triangle = 2435.69 sq. units

Note: sqrt() function is used to calculate the square root of any number.

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

1 comment: