Posted on by Kalkicode
Code Geometric

Find the triangle area by using sas

The area of a triangle can be calculated using various methods, one of which is the Side-Angle-Side (SAS) method. In this method, you use two side lengths and the included angle to find the area of the triangle. The SAS method is particularly useful when you have the length of two sides and the angle between them and want to calculate the area of the triangle formed by those sides.

Problem Statement

Given two side lengths (a and b) and the included angle (c) between them, the task is to calculate the area of the triangle using the SAS method. The formula for calculating the area of a triangle using the SAS method is:

Area = (a * b * sin(c)) / 2

Example Scenario

Imagine you are working on a construction project and need to calculate the area of a triangular piece of land using the lengths of two sides and the angle between them. The SAS method would be ideal for this situation.

Idea to Solve the Problem

Calculate SAS Triangle Area

To solve this problem, you can follow these steps:

  1. Accept the side lengths (a and b) and the included angle (c) in degrees as inputs.
  2. Convert the angle from degrees to radians using the formula radians = degrees * (M_PI / 180.0).
  3. Use the provided formula to calculate the area of the triangle.
  4. Display the calculated area.

Pseudocode

function sas_triangle_area(a, b, c):
    radians = c * (M_PI / 180.0)
    area = (a * b * sin(radians)) / 2
    return area

main:
    a1, b1, c1 = 31.4, 29.3, 41
    a2, b2, c2 = 17.9, 25.1, 98
    a3, b3, c3 = 16.8, 33.6, 77
    
    area1 = sas_triangle_area(a1, b1, c1)
    area2 = sas_triangle_area(a2, b2, c2)
    area3 = sas_triangle_area(a3, b3, c3)
    
    print("Triangle [ Side a :", a1, "Side b :", b1, "Angle c :", c1, "]")
    print("Area :", area1)
    
    print("Triangle [ Side a :", a2, "Side b :", b2, "Angle c :", c2, "]")
    print("Area :", area2)
    
    print("Triangle [ Side a :", a3, "Side b :", b3, "Angle c :", c3, "]")
    print("Area :", area3)

Algorithm Explanation

  1. Define a function sas_triangle_area that takes side lengths (a and b) and the included angle (c) as inputs.
  2. Convert the angle from degrees to radians using the provided formula.
  3. Calculate the area of the triangle using the SAS formula and return the result.
  4. In the main function, set three test cases with different side lengths and angles.
  5. Calculate the areas for each test case by calling the sas_triangle_area function.
  6. Display the calculated areas along with their respective side lengths and angles.

Code Solution

//C Program 
//Find the area of sas triangle
#include <stdio.h>

#include <math.h>

//Find area of triangle using given sides a, b and angle c
void sas_triangle_area(double a, double b, int c)
{
	// a and b is side
	// c is angle
	/*
	    Area of sas triangle 
	   
	          ab sin c
	        --------
	            2
	*/
	// Convert given degrees to radians
	double radians = c * (M_PI / 180.0);
	double area = (a * b * sin(radians)) / 2;
	printf(" Triangle  [ a : %lf, b : %lf c : %d ] ", a, b, c);
	printf("\n Area : %lf\n\n", area);
}
int main()
{
	//Test Cases
	sas_triangle_area(31.4, 29.3, 41);
	sas_triangle_area(17.9, 25.1, 98);
	sas_triangle_area(16.8, 33.6, 77);
	return 0;
}

Output

 Triangle  [ a : 31.400000, b : 29.300000 c : 41 ]
 Area : 301.793714

 Triangle  [ a : 17.900000, b : 25.100000 c : 98 ]
 Area : 222.458770

 Triangle  [ a : 16.800000, b : 33.600000 c : 77 ]
 Area : 275.006207
/*
  Java program
  Find the area of sas triangle
*/
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	public void sas_triangle_area(double a, double b, int c)
	{
		// a and b is side
	    // c is angle
	    /*
	        Area of sas triangle 
	       
	              ab sin c
	            --------
	                2
	    */
	    // Convert given degrees to radians
		double radians = c * (Math.PI / 180.0);
		
		double area = (a * b * Math.sin(radians)) / 2;

		System.out.print(" Triangle [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		System.out.print("\n Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		TriangleArea obj = new TriangleArea();

		    obj.sas_triangle_area(31.4,29.3,41);

		    obj.sas_triangle_area(17.9,25.1,98);

		    obj.sas_triangle_area(16.8,33.6,77);
	}
}

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.79371392592327

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.45877030245006

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.0062070849848
/*
  C++ program
  Find the area of sas triangle
*/
#include<iostream>
#include<math.h>
using namespace std;
class TriangleArea
{
	public:
		//Find area of triangle using given sides a, b and angle c
		void sas_triangle_area(double a, double b, int c)
		{
			// a and b is side
			// c is angle
			/*
				        Area of sas triangle 
				       
				              ab sin c
				            --------
				                2
				    */
			// Convert given degrees to radians
			double radians = c * (M_PI / 180.0);
			double area = (a * b * sin(radians)) / 2;
			cout << " Triangle [ a : " << a << ", b : " << b << " c : " << c << " ] ";
			cout << "\n Area : " << area << "\n\n";
		}
};
int main()
{
	TriangleArea obj = TriangleArea();
	obj.sas_triangle_area(31.4, 29.3, 41);
	obj.sas_triangle_area(17.9, 25.1, 98);
	obj.sas_triangle_area(16.8, 33.6, 77);
	return 0;
}

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.794

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.459

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.006
/*
  C# program
  Find the area of sas triangle
*/
using System;
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	public void sas_triangle_area(double a, double b, int c)
	{
		// a and b is side
		// c is angle
		/*
			        Area of sas triangle 
			       
			              ab sin c
			            --------
			                2
			    */
		// Convert given degrees to radians
		double radians = c * (Math.PI / 180.0);
		double area = (a * b * Math.Sin(radians)) / 2;
		Console.Write(" Triangle [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		Console.Write("\n Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		TriangleArea obj = new TriangleArea();
		obj.sas_triangle_area(31.4, 29.3, 41);
		obj.sas_triangle_area(17.9, 25.1, 98);
		obj.sas_triangle_area(16.8, 33.6, 77);
	}
}

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.793713925923

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.45877030245

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.006207084985
<?php
/*
  Php program
  Find the area of sas triangle
*/
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	public	function sas_triangle_area($a, $b, $c)
	{
		// a and b is side
		// c is angle
		/*
			        Area of sas triangle 
			       
			              ab sin c
			            --------
			                2
			    */
		// Convert given degrees to radians
		$radians = $c * (M_PI / 180.0);
		$area = ($a * $b * sin($radians)) / 2;
		echo " Triangle [ a : ". $a .", b : ". $b ." c : ". $c ." ] ";
		echo "\n Area : ". $area ."\n\n";
	}
}

function main()
{
	$obj = new TriangleArea();
	$obj->sas_triangle_area(31.4, 29.3, 41);
	$obj->sas_triangle_area(17.9, 25.1, 98);
	$obj->sas_triangle_area(16.8, 33.6, 77);
}
main();

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.79371392592

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.45877030245

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.00620708498
/*
  Node Js program
  Find the area of sas triangle
*/
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	sas_triangle_area(a, b, c)
	{
		// a and b is side
		// c is angle
		/*
			        Area of sas triangle 
			       
			              ab sin c
			            --------
			                2
			    */
		// Convert given degrees to radians
		var radians = c * (Math.PI / 180.0);
		var area = (a * b * Math.sin(radians)) / 2;
		process.stdout.write(" Triangle [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		process.stdout.write("\n Area : " + area + "\n\n");
	}
}

function main()
{
	var obj = new TriangleArea();
	obj.sas_triangle_area(31.4, 29.3, 41);
	obj.sas_triangle_area(17.9, 25.1, 98);
	obj.sas_triangle_area(16.8, 33.6, 77);
}
main();

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.79371392592327

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.45877030245006

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.0062070849848
#   Python 3 program
#   Find the area of sas triangle
import math

class TriangleArea :
	# Find area of triangle using given sides a, b and angle c
	def sas_triangle_area(self, a, b, c) :
		#  a and b is side
		#  c is angle
		# 
		# 	        Area of sas triangle 
		# 	       
		# 	              ab sin c
		# 	            --------
		# 	                2
		# 	    
		
		#  Convert given degrees to radians
		radians = c * (math.pi / 180.0)
		area = (a * b * math.sin(radians)) / 2
		print(" Triangle [ a : ", a ,", b : ", b ," c : ", c ," ] ", end = "")
		print("\n Area : ", area ,"\n\n", end = "")
	

def main() :
	obj = TriangleArea()
	obj.sas_triangle_area(31.4, 29.3, 41)
	obj.sas_triangle_area(17.9, 25.1, 98)
	obj.sas_triangle_area(16.8, 33.6, 77)

if __name__ == "__main__": main()

Output

 Triangle [ a :  31.4 , b :  29.3  c :  41  ]
 Area :  301.79371392592327

 Triangle [ a :  17.9 , b :  25.1  c :  98  ]
 Area :  222.45877030245006

 Triangle [ a :  16.8 , b :  33.6  c :  77  ]
 Area :  275.0062070849848
#   Ruby program
#   Find the area of sas triangle

class TriangleArea

	# Find area of triangle using given sides a, b and angle c
	def sas_triangle_area(a, b, c)
	
		#  a and b is side
		#  c is angle
		# 
		# 	        Area of sas triangle 
		# 	       
		# 	              ab sin c
		# 	            --------
		# 	                2
		# 	    
		
		#  Convert given degrees to radians
		radians = c * (Math::PI / 180.0)
		area = (a * b * Math.sin(radians)) / 2
		print(" Triangle [ a : ", a ,", b : ", b ," c : ", c ," ] ")
		print("\n Area : ", area ,"\n\n")
	end
end
def main()

	obj = TriangleArea.new()
	obj.sas_triangle_area(31.4, 29.3, 41)
	obj.sas_triangle_area(17.9, 25.1, 98)
	obj.sas_triangle_area(16.8, 33.6, 77)
end
main()

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ] 
 Area : 301.79371392592327

 Triangle [ a : 17.9, b : 25.1 c : 98 ] 
 Area : 222.45877030245006

 Triangle [ a : 16.8, b : 33.6 c : 77 ] 
 Area : 275.0062070849848

/*
  Scala program
  Find the area of sas triangle
*/
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	def sas_triangle_area(a: Double, b: Double, c: Int): Unit = {
		// a and b is side
		// c is angle
		/*
			        Area of sas triangle 
			       
			              ab sin c
			            --------
			                2
			    */
		// Convert given degrees to radians
		var radians: Double = c * (Math.PI / 180.0);
		var area: Double = (a * b * Math.sin(radians)) / 2;
		print(" Triangle [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		print("\n Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: TriangleArea = new TriangleArea();
		obj.sas_triangle_area(31.4, 29.3, 41);
		obj.sas_triangle_area(17.9, 25.1, 98);
		obj.sas_triangle_area(16.8, 33.6, 77);
	}
}

Output

 Triangle [ a : 31.4, b : 29.3 c : 41 ]
 Area : 301.79371392592327

 Triangle [ a : 17.9, b : 25.1 c : 98 ]
 Area : 222.45877030245006

 Triangle [ a : 16.8, b : 33.6 c : 77 ]
 Area : 275.0062070849848
/*
  Swift program
  Find the area of sas triangle
*/
import Foundation
class TriangleArea
{
	//Find area of triangle using given sides a, b and angle c
	func sas_triangle_area(_ a: Double, _ b: Double, _ c: Int)
	{
		// a and b is side
		// c is angle
		/*
			        Area of sas triangle 
			       
			              ab sin c
			            --------
			                2
			    */
		// Convert given degrees to radians
		let radians: Double = Double(c) * (Double.pi / 180);
		let area: Double = (a * b * sin(radians)) / 2;
		print(" Triangle [ a : ", a ,", b : ", b ," c : ", c ," ] ", terminator: "");
		print("\n Area : ", area ,"\n\n", terminator: "");
	}
}
func main()
{
	let obj: TriangleArea = TriangleArea();
	obj.sas_triangle_area(31.4, 29.3, 41);
	obj.sas_triangle_area(17.9, 25.1, 98);
	obj.sas_triangle_area(16.8, 33.6, 77);
}
main();

Output

 Triangle [ a :  31.4 , b :  29.3  c :  41  ]
 Area :  301.793713925923

 Triangle [ a :  17.9 , b :  25.1  c :  98  ]
 Area :  222.45877030245

 Triangle [ a :  16.8 , b :  33.6  c :  77  ]
 Area :  275.006207084985

Output Explanation

The code calculates the area for each test case using the SAS method and displays the results. For a triangle with side lengths (a, b) = (31.4, 29.3) and included angle (c) = 41, the area is approximately 301.793714. Similarly, for other test cases, the areas are calculated and displayed.

Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic and trigonometric operations (sin) that are computed in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment