For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
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.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.