C program to find area of an equilateral triangle

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 equilateral 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 concepts.

The mathematical formula for area of equilateral triangle in programming notation can be written as (sqrt(3) / 4) * (side * side). Where sqrt() is a function used to compute square root. We will 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;
}

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

Output

Enter side of an equilateral triangle: 75
Area of equilateral triangle = 2435.69 sq. units

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>