C Program To Calculate Area of a Triangle using Pointers

Lets write a C program to calculate area of a Triangle when their sides are input by the user. Lets use pointers and functions.

Problem Statement

If the lengths of the sides of a Triangle are denoted by a, b and c, then area of Triangle is given by:

Heron's or Hero's Formula

where, s = (a + b + c) / 2. Write a function to calculate the area of triangle.

Here s is semi-perimeter of the Triangle.

Related Read:
Find Area of a Triangle Using Its Sides: C Program
Basics of Pointers In C Programming Language

Formula To Calculate Area of a Triangle When its Sides are Given

area = sqrt( S * (S – a) * (S – b) * (S – c) );

where a, b and c are lengths of sides of the Triangle.
S = (a + b + c) / 2.0;
S – Semi-perimeter.

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Calculate Area of Triangle using Pointers


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

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


Source Code: C Program To Calculate Area of Triangle using Pointers

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

void cal_area(float, float, float, float*);
int  validate(float, float, float);

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

    printf("Enter values of 3 sides of a Triangle.\n");
    scanf("%f%f%f", &a, &b, &c);

    if( validate(a, b, c) )
    {
        cal_area(a, b, c, &area);
        printf("Area of Triangle is %0.2f\n", area);
    }
    else
    {
        printf("Please enter valid values for sides of Triangle.\n");
    }

    return 0;
}

void cal_area(float x, float y, float z, float *A)
{
    float S;

     S = (x + y + z) / 2.0;

    *A = sqrt(S * (S - x) * (S - y) * (S - z));
}

int validate(float x, float y, float z)
{
    int flag = 0;

    if(x > y && x > z)
    {
        flag = ( x < (y + z) );
    }
    else if(y > z)
    {
        flag = ( y < (x + z) );
    }
    else
    {
        flag = ( z < (x + y) );
    }

    return(flag);
}

Output 1:
Enter values of 3 sides of a Triangle.
10
20
30
Please enter valid values for sides of Triangle.

Output 2:
Enter values of 3 sides of a Triangle.
50
30
0
Please enter valid values for sides of Triangle.

Output 3:
Enter values of 3 sides of a Triangle.
15
14
2
Area of Triangle is 12.53

Output 4:
Enter values of 3 sides of a Triangle.
14.6
14
0.7
Area of Triangle is 2.58

Output 5:
Enter values of 3 sides of a Triangle.
4
5
6
Area of Triangle is 9.92

In Output 1: the largest side is 30. So the other side is 10 and 20. Adding 10 and 20 gives 30, which is not greater than the largest side. So these three values can’t form a valid triangle.

In Output 2: we’ve a 0. No side of a Triangle can have a length of 0. So it’s a invalid Triangle.

Logic To Calculate Area of a Triangle using Pointers

We ask the user to enter/input values of 3 sides of a Triangle. We pass these three values along with the address of variable area to a function called cal_area().

Inside cal_area() function we calculate the semi-perimeter using below formula:

S = (x + y + z) / 2.0;

Note: While calculating Semi-perimeter make sure to divide by 2.0 and not by 2. Because sum of sides of triangle might be a floating point number. Division by integer will give integer value as result. So it’s always better to avoid division by integer value.

Calculate Area of Triangle using Heron’s or Hero’s Formula

To calculate Area of a Triangle when all 3 of its sides are known, we make use of Heron’s or Hero’s Formula:

*A= sqrt( S * (S – a) * (S – b) * (S – c) );

We are storing the result inside a pointer variable. Variable A has the address of variable area i.e., A = &area. *A is the value present at address A or &area.

When we modify the value present at an address it reflects everywhere in the program. Hence we are able to display the result of area inside main method, without literally returning any value from cal_area() funtion.

Logic To Validate The Sides of Triangle

A Triangle is said to be valid if the sum of two sides is greater than the largest of the three sides.

For 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, value of (a+b) must be greater than c.

(a+b) > c

Triangle Valid or Not based On Sides: C Program

In function validate() we first find the largest side of the triangle and check if its value is less than the sum of other 2 sides of the triangle. If true we return 1 else we return 0.

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

require attribute: HTML5

Lets see how require attribute of HTML5 works.

require attribute forces the user to enter some value. But it does not validate the user input.
Example: It makes sure if the email field has some value in it but doesn’t check if the user has entered valid email address or not. It only makes sure, the required field isn’t left empty.

require-form-field-attribute-html5

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< !DOCTYPE html>
<html>
<head>
<title>required attribute: HTML5</title>
<link href="myStyle.css" rel="stylesheet"/>
</head>
<body>
 
<form>
<label for="name">Name: </label>
 <input type="text" name="name" required/><br />
 
<label for="age">Age: </label>
 <input type="text" name="age"/><br />
 
 <input type="submit"/> 
</form>
 
</body>
</html>

Here we have 2 input fields. First input field has been made required, by using require attribute.

CSS file associated with this is same as present at pattern and title Attribute of Form Field: HTML5

require attribute: HTML5


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

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



Note:
Server side validation is also equally important. Because someone might directly pass the value via the script present in the action field of form tag – if form uses GET method to pass user data.

Also note that, IE(Internet Explorer) doesn’t support require attribute, so you can make use of Javascript or jQuery to make sure IE users are taken care of.
Related Read: Registration Form Validation: PHP + jQuery + AJAX