Tutorial Playlist
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.
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.
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:
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.
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:
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.
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.
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;
}
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;
}
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;
}
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;
}
Code:
<?php Â
    $base = 2 ; Â
    $height = 4 ;  Â
    $area =  ($base * $height) / 2; Â
    echo("Area of Triangle is: "); Â
    echo($area); Â
?> Â
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); Â
  }} Â
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); Â
  }}
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.
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).
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.
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.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...