Find Area of an Equilateral Triangle: C Program

Given the sides of the Triangle, write a C program to calculate Perimeter and Area of a equilateral Triangle.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Note:
Equilateral Triangle: A Triangle is called equilateral triangle if length of 3 sides of it are equal.
Example: a = 10, b = 10, c = 10;

So we ask the user to input value of the side only once.

Formula To Calculate Perimeter and Area of Equilateral Triangle

Perimeter = a + a + a;
i.e., Perimeter = 3 * a;

Area = ( ( sqrt(3) / 4 ) * pow(a, 2));

where a is the value of side of the Equilateral Triangle.

Find Area of an Equilateral Triangle: C Program


[youtube https://www.youtube.com/watch?v=ftjjhBLrLk4]

YouTube Link: https://www.youtube.com/watch?v=ftjjhBLrLk4 [Watch the Video In Full Screen.]


Find Area of an Equilateral Triangle: C Program

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

int main()
{
    float sides, area, p;

    printf("Enter value for side of Equilateral Triangle\n");
    scanf("%f", &sides);

    p    = 3 * sides;
    area = ( (sqrt(3)/4) * pow(sides, 2) );

    printf("Perimeter is %f\n", p);
    printf("Area is %f\n", area);

    return 0;
}

Output:
Enter value for side of Equilateral Triangle
75
Perimeter is 225.000000
Area is 2435.696533

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene

If the three sides of a Triangle are entered through the keyboard, write a program to check whether the Triangle is isosceles, equilateral, scalene Triangle.

Note:
Equilateral Triangle: A Triangle is called equilateral triangle if length of 3 sides of it are equal.
Example: a = 10, b = 10, c = 10;

Isosceles Triangle: A Triangle is called isosceles triangle if length of 2 sides of it are equal.
Example: a = 5, b = 10, c = 10;

Scalene Triangle: A Triangle is called scalene triangle if length of all 3 sides are different or not equal.
Example: a = 5, b = 6, c = 10;

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

        if(a == b && b == c)
        {
            printf("It's an Equilateral Triangle\n");
        }
        else if(a == b || a == c || b == c)
        {
            printf("It's an Isosceles Triangle\n");
        }
        else
        {
            printf("It's a Scalene Triangle\n");
        }

    return 0;
}

Output 1:
Enter values for a, b and c
10
10
10
It’s an Equilateral Triangle

Output 2:
Enter values for a, b and c
20
20
30
It’s an Isosceles Triangle

Output 3:
Enter values for a, b and c
10
15
8
It’s a Scalene Triangle

Here a, b and c are lengths of sides of the triangle. First we check if length of a is equal to length of b and length of c. If that’s true, then its Equilateral Triangle.

Next, if the first condition is false – we check if either length of a is equal to length of b or length of a is equal to length of c or length of b is equal to length of c. If any of these conditions are true, then 2 sides of the Triangle are equal. So its an Isosceles Triangle.

If above two conditions are false, then length of all sides of the Triangle is different or not equal, so it’s a Scalene Triangle.

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene


[youtube https://www.youtube.com/watch?v=m8vF8qvFvxE]

YouTube Link: https://www.youtube.com/watch?v=m8vF8qvFvxE [Watch the Video In Full Screen.]


Check If Triangle is valid or not

First we ask the user to input lengths of 3 sides of the Triangle. We’ll need to write the C program to check if the entered values form a valid Triangle or not. You can find the logic to it at Triangle Valid or Not based On Sides: C Program.

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene: Valid Triangle

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c)
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
    }

    if(flag)
    {
        if(a == b && b == c)
        {
            printf("It's an Equilateral Triangle\n");
        }
        else if(a == b || a == c || b == c)
        {
            printf("It's an Isosceles Triangle\n");
        }
        else
        {
            printf("It's a Scalene Triangle\n");
        }
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
4
5
6
It’s a Scalene Triangle

Output 2:
Enter values for a, b and c
30
30
30
It’s an Equilateral Triangle

Output 3:
Enter values for a, b and c
10
50
50
It’s an Isosceles Triangle

Output 4:
Enter values for a, b and c
10
50
20
Invalid Triangle

Note:
Result of output 4 gives “Invalid Triangle” because – the largest side is 50. So the sum of other two sides must be greater than 50. (10+20) = 30. So 30 is not equal to 50, so Triangle can’t be formed with length of sides as 10, 50 and 20.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle

Lets ask the user to input lengths of 3 sides of the Triangle. First lets check if those 3 values form a valid Triangle. If it does, then we shall calculate perimeter, semi-perimeter and area of the Triangle. If it doesn’t form a valid Triangle, then we display that message on to the console window.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Logic To check if it’s a valid Triangle or Not

Logic to find if the user entered values for sides of Triangle actually forms a valid Triangle or not is present in our previous video tutorial. Please watch it without fail before continuing this tutorial. We’ve posted source code, video and explanation at Triangle Valid or Not based On Sides: C Program

Formula To Calculate Perimeter, Semi-perimeter and Area of a Triangle

p = (a + b + c);
sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
p – perimeter
sp – Semi-perimeter.

C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle


[youtube https://www.youtube.com/watch?v=CmGb-nlSC7o]

YouTube Link: https://www.youtube.com/watch?v=CmGb-nlSC7o [Watch the Video In Full Screen.]


C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle

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

int main()
{
    float a, b, c, flag = 0, p, sp, area;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c)
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
        printf("%f\n", flag);
    }

    if(flag)
    {
        p    = a + b + c;
        sp   = p / 2.0;
        area = sqrt( sp*(sp-a)*(sp-b)*(sp-c) );

        printf("Perimeter is %f\n", p);
        printf("Semi-Perimeter is %f\n", sp);
        printf("Area of Triangle is %f\n", area);
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
5
3
Invalid Triangle

Output 2:
Enter values for a, b and c
5
6
9
Perimeter is 20.000000
Semi-Perimeter is 10.000000
Area of Triangle is 14.142136

In above source code, variable flag stores true(1) or false(0) value.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Triangle Valid or Not based On Sides: C Program

If the three sides of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of two sides is greater than the largest of the three sides.

Related Read:
Basic Arithmetic Operations In C
Find Area of a Triangle Using Its Sides: C Program
Triangle Valid or Not based On Angles: C Program

Logic To Find Valid Triangle or Not

First we find out biggest side in the 3 sides of the triangle. Next we add the other 2 sides. Now the addition of the other 2 sides must be greater than the biggest side of the Triangle, for a Triangle to be valid. If not, its not a Triangle.

Example:
If a, b and c are 3 sides of the Triangle. If c is the largest side. Then for the Triangle to be valid, (a+b) must be greater than c.
(a+b) > c
If this is true, then Triangle is valid, if not, its invalid Triangle.

Triangle Valid or Not based On Sides: C Program


[youtube https://www.youtube.com/watch?v=XMJdWL_FDck]

YouTube Link: https://www.youtube.com/watch?v=XMJdWL_FDck [Watch the Video In Full Screen.]


Triangle Valid or Not based On Sides: C Program

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c )
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
    }

    if(flag)
    {
        printf("Valid Triangle\n");
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
4
3
Invalid Triangle

Output 2:
Enter values for a, b and c
10
15
6
Valid Triangle

In above source code, variable flag stores true(1) or false(0) value.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find Area of a Triangle Using Its Sides: C Program

If lengths of three sides of a Triangle are input through the keyboard, write a program to find the area of the Triangle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program

Note:A Triangle is valid if the sum of 2 sides of the Triangle is greater than the largest of the 3 sides.

Important: In this program we assume that the user has entered valid lengths of the Triangle.

Formula To Calculate Area and Semi-perimeter of a Triangle

sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
sp – Semi-perimeter.

Note: In geometry, above formula to calculate area of Triangle is called Heron’s formula (sometimes called Hero’s formula).

Find Area of a Triangle Using Its Sides: C Program


[youtube https://www.youtube.com/watch?v=whGefMyrmVM]

YouTube Link: https://www.youtube.com/watch?v=whGefMyrmVM [Watch the Video In Full Screen.]


Find Area of a Triangle Using Its Sides: C Program

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

int main()
{
    float a, b, c, sp, area;

    printf("Enter values of a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    sp = (a+b+c)/2.0;

    area = sqrt(sp*(sp-a)*(sp-b)*(sp-c));

    printf("Area of triangle is %f\n", area);

    return 0;
}

Output:
Enter values of a, b and c
4
5
6
Area of triangle is 9.921567

Note: While calculating Semi-perimeter, make sure to divide by 2.0 and not by 2. Because division by integer number returns only the integer part of the result. So for certain inputs the result might be wrong. So always use the floating/double value while dividing in C Programs.

Note: Since we are using sqrt() method, we need to include math.h library file (header file) without fail.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert