top

Search

Software Key Tutorial

.

UpGrad

Software Key Tutorial

Program To Find Area Of Triangle

Introduction

It is essential to comprehend triangle properties in various fields, including engineering and architecture, as well as mathematics and physics. Basic geometric forms include triangles. The area of a triangle is one of its most basic yet important characteristics. An area is useful for various practical purposes and refers to the area that a triangle's three sides enclose.

Calculating a triangle's area is simple but requires a fundamental understanding of geometry. The program to find the area of a triangle teaches us a straightforward yet practical technique for calculating a triangle's surface area from the lengths of its three sides.

This tutorial will guide you to write a program to find the area of triangles in C. By learning and using this application, you will understand triangle geometry and have a useful tool to quickly and accurately locate their locations. 

Overview

Here is a detailed overview and some key points before you master writing a program to find the area of a triangle with 3 sides:

Calculation: We apply the area formula when we know the base and height: Area = (1/2) * base * height. The division by 2 is significant because the area of a triangle is half that of a rectangle formed by the base and height.

Unit of Measurement: It is critical to analyze the unit of measurement for the input and output data. For example, if the base and height are in centimeters, the area will be in square centimeters.

Data Types: The data types used to record the base, height, and area values will vary depending on the computer language. As triangles may have non-integer dimensions and the output might be a fractional number, standard options include floating-point data types (e.g., float or double) for the base, height, and area values.

Program To Find Area Of Triangle

Code:

#include <stdio.h>
    
int main()
{
    // initializing the base and height
    float b = 5, h = 12;

    // calculating the area
    float area_of_triangle = ((float)1/2) * b * h;

    // printing the area 
    printf("The area of given Triangle is : %f", area_of_triangle);
    
    return 0;
}

Explanation:

  • #include <stdio.h>: This line includes the standard input-output library (stdio.h) which provides functions like printf and scanf for input and output operations.

  • int main(): This is the main function of the program. Every C program must have a main function as the starting point of execution.

  • float b = 5, h = 12: Two variables b and h are declared and initialised with the values 5 and 12, respectively. These represent the base and height of the triangle, and they are declared as floating-point variables (float) to support decimal values.

  • float area_of_triangle = ((float)1/2) * b * h: The area of the triangle is calculated using the formula (1/2) * base * height. However, there's a small issue in this line. The expression (1/2) performs integer division since both 1 and 2 are integers, resulting in 0. To fix this, the (float) cast is used to perform floating-point division. So, the correct calculation becomes ((float)1/2) * b * h, which gives the expected result.

  • printf("The area of a given Triangle is : %f", area_of_triangle): The calculated area is printed on the console using the printf function. %f is a format specifier used to display floating-point values. The area_of_triangle variable's value will be inserted in place of %f.\

  • return 0: The return statement is used to exit the main() function. The value 0 is returned to the operating system, indicating successful execution.

When you run this program, it will calculate the area of the triangle with a base of 5 units and height of 12 units and display the output:30.000000

The area is 30.000000 since printf displays floating-point numbers with six decimal places by default. This means the area of the triangle is 30 square units.

Algorithm To Find Area Of Triangle

To find the area of a triangle, you can use the formula:

Area = (base * height) / 2

Here's a simple algorithm to find the area of a triangle:

  1. Start

  2. Input the base and height of the triangle.

  3. Calculate the area using the formula: Area = (base * height) / 2.

  4. Output the calculated area.

  5. End

Here is the algorithm in pseudocode:

Algorithm to Find the Area of a Triangle:

1. Start

2. Input base, height

3. area = (base * height) / 2

4. Output "The area of the triangle is: " + area

5. End

This algorithm will allow you to calculate the area of any triangle given its base and height.

Complexity To Find Area Of Triangle

The complexity of finding the area of a triangle is considered to be constant time complexity, O(1).

The reason for this is that calculating the area of a triangle involves simple arithmetic operations - multiplication and division - which take constant time regardless of the size of the input (in this case, the base and height of the triangle). The formula to calculate the area of a triangle, (base * height) / 2, does not depend on the number of elements in the input.

Regardless of the size of the triangle's base and height, the time taken to calculate the area remains constant. Hence, the time complexity is O(1). It's important to note that constant time complexity does not mean the program runs instantly, but rather that the execution time does not grow with the input size.

C Program to Find Area of Triangle Using its Coordinates

Code:

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

int main()
{
    // initializing the coordinates
    float x1 = 3, y1 = 3;
    float x2 = 5, y2 = 2;
    float x3 = 4, y3 = 6;
    
    // alternatively you can also take the coordinates as user input 

    float area = ((float)1 / 2) * abs((int) (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)));
    printf("Area of the given Triangle is : %f", area);

    return 0;
}

 C Program to find the Area of Equilateral Triangle

Code:

#include <stdio.h>
#include <math.h> // for sqrt() function
    
int main()
{
    // initialzing the side of the equilateral triangle
    float side = 5;

    // calculating the area, sqrt() is a built in function used to find the square root of numbers
    float area = (sqrt(3)/4) * (side * side);

    // printing the area
    printf("The area of the Triangle is : %f", area);
    
    return 0;
}

C Program to Find Area of Triangle Using Heron's Formula

Code:

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

int main()
{
    // intializing the sides and calculating the semi-perimeter
    float a = 6, b = 6, c = 4;
    float s = (a + b + c) / 2;

    // calculating the area
    float area = sqrt(s * (s - a) * (s - b) * (s - c));
    
    // printing the area
    printf("The area of the triangle is :  %f ", area);

    return 0;
}

C Program to Find Area of Triangle Using a Function

Code:

#include <stdio.h>

float area_of_triangle(float base, float height)
{
    float area = ((float)1 / 2) * base * height;
    return area;
}

int main()
{
    // initializing the base and height
    float base = 6, height = 18;

    // calling the function to calculate the area
    float area = area_of_triangle(base, height);

    // printing the area
    printf("The area of given Triangle is : %f", area);

    return 0;
}

PHP Program To Find Area Of Triangle

Code:

<?php  
       $base = 2 ;  
       $height = 4 ;   
       $area =   ($base * $height) / 2;  
       echo("Area of Triangle is: ");  
       echo($area);  
?>  

C# Program To Find Area Of Triangle

Code:

using System;  
class main  
{   static void Main()   
    {  
      float  x = 2,y =4 ;  
      double area =  (x*y) / 2 ;   
      Console.WriteLine("Area of Triangle is :"+area);  
    }}  

Java Program To Find Area Of Triangle

Code:

public class test  
 {  
   public static void main (String args[])  
    {          float base=2,height =4,area ;  
                         area = ( base*height) / 2 ;  
                         System.out.println("Area of Triangle is: "+area);  
    }}

Conclusion

In this tutorial, we discovered how to compute the area of a triangle using a C program depending on its base and height. We also learned how to expand the program to find the area using different approaches like Heron's formula. We highlighted the necessity of learning the basic principles of geometry and how they apply to a C program to find the area of a triangle. This knowledge may be immensely beneficial in tackling numerous real-world situations that entail geometric computations.

FAQs

  1. How can you write a Python program to find the area of a triangle using a class?

To calculate the area of a triangle in Python, create a Triangle class with the necessary methods and attributes. You may add a method within the class that accepts the base and height of the triangle as input and then computes the area using the formula (1/2 * base * height).

  1. What happens if the given side lengths cannot form a valid triangle?

Before implementing Heron's formula to find the area of the triangle program in C, the program examines whether the specified side lengths may form a valid triangle. It achieves so by testing if the sum of any two sides is higher than the third side (Triangle Inequality Theorem). The application will print an error message if the input side lengths cannot form a proper triangle.

  1. Can the program handle negative or zero values for the side lengths?

The program should check the input and look for negative or zero side lengths. It should reject negative or zero numbers and encourage users to supply reasonable positive side lengths.

Leave a Reply

Your email address will not be published. Required fields are marked *