1. Home

A Comprehensive Guide on Software Key Tutorials

Elevate your software skills with our comprehensive tutorials covering concepts from basic to advanced techniques. Dive in and become a software pro!

• 220 Lessons
• 37 Hours
155.Â

# Program To Find Area Of Triangle

Updated on 19/07/20242,182 Views

## 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() functionint 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.

Director of Engineering

Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working â€¦Read More

###### Get Free Career Counselling
+91
*
By clicking, I accept theT&Cand
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.

Talk to our experts. Weâ€™re available 24/7.

Indian Nationals

1800 210 2020

Foreign Nationals

+918045604032

## Disclaimer

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