Calculate area of triangle

Here given code implementation process.

// C Program
// Calculate area of triangle
#include <stdio.h>
#include <math.h>

//Calculate area of triangle by given three side
void triangle_area(double a,double b,double c)
{
 
  double side = (a + b + c)/2;

  //Calculate area of triangle
  double area = sqrt(side *((side-a)*(side-b)*(side-c)));

  //Display the result of triangle area
  printf(" Triangle Sides [ a : %lf, b : %lf c : %lf ] ",a,b,c );
  printf("\n Triangle Area : %lf\n\n", area);
}

int main()
{
  /*

    24.2        .
      ③      .   . ②
          .        .   13.1 
      .              .
    -------------------       
          ①
             24
  */
  //Test Cases  
  triangle_area(24,13.1,24.2);

  triangle_area(22.2,20.2,7.7);

  triangle_area(12.8,13.7,15.9);

  return 0;   
}

Output

 Triangle Sides [ a : 24.000000, b : 13.100000 c : 24.200000 ]
 Triangle Area : 151.895350

 Triangle Sides [ a : 22.200000, b : 20.200000 c : 7.700000 ]
 Triangle Area : 77.508067

 Triangle Sides [ a : 12.800000, b : 13.700000 c : 15.900000 ]
 Triangle Area : 84.134892
/*
  Java program
  Calculate area of triangle
*/
public class TriangleArea
{
	//Calculate area of triangle by given three side
	public void triangle_area(double a, double b, double c)
	{
		double side = (a + b + c) / 2;
		//Calculate area of triangle
		double area = Math.sqrt(side * ((side - a) * (side - b) * (side - c)));
		//Display the result of triangle area
		System.out.print(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		System.out.print("\n Triangle Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		TriangleArea obj = new TriangleArea();
		/*

		  24.2        .
		    ③      .   . ②
		        .        .   13.1 
		    .              .
		  -------------------       
		        ①
		           24
		*/
		//Test Cases  
		obj.triangle_area(24, 13.1, 24.2);
		obj.triangle_area(22.2, 20.2, 7.7);
		obj.triangle_area(12.8, 13.7, 15.9);
	}
}

Output

 Triangle Sides [ a : 24.0, b : 13.1 c : 24.2 ] 
 Triangle Area : 151.89535030325973

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ] 
 Triangle Area : 77.50806663664115

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ] 
 Triangle Area : 84.13489169185397
/*
  C++ program
  Calculate area of triangle
*/
#include<iostream>
#include <math.h>
using namespace std;
class TriangleArea
{
	public:
		//Calculate area of triangle by given three side
		void triangle_area(double a, double b, double c)
		{
			double side = (a + b + c) / 2;
			//Calculate area of triangle
			double area = sqrt(side *((side - a) *(side - b) *(side - c)));
			cout << " Triangle Sides [ a : " << a << ", b : " << b << " c : " << c << " ] ";
			cout << "\n Triangle Area : " << area << "\n\n";
		}
};
int main()
{
	TriangleArea obj = TriangleArea();
	/*

			  24.2        .
			    ③      .   . ②
			        .        .   13.1 
			    .              .
			  -------------------       
			        ①
			           24
			*/
	//Test Cases  
	obj.triangle_area(24, 13.1, 24.2);
	obj.triangle_area(22.2, 20.2, 7.7);
	obj.triangle_area(12.8, 13.7, 15.9);
	return 0;
}

Output

 Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
 Triangle Area : 151.895

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
 Triangle Area : 77.5081

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
 Triangle Area : 84.1349
/*
  C# program
  Calculate area of triangle
*/
using System;
public class TriangleArea
{
	//Calculate area of triangle by given three side
	public void triangle_area(double a, double b, double c)
	{
		double side = (a + b + c) / 2;
		//Calculate area of triangle
		double area = Math.Sqrt(side * ((side - a) * (side - b) * (side - c)));
		Console.Write(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		Console.Write("\n Triangle Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		TriangleArea obj = new TriangleArea();
		/*

				  24.2        .
				    ③      .   . ②
				        .        .   13.1 
				    .              .
				  -------------------       
				        ①
				           24
				*/
		//Test Cases  
		obj.triangle_area(24, 13.1, 24.2);
		obj.triangle_area(22.2, 20.2, 7.7);
		obj.triangle_area(12.8, 13.7, 15.9);
	}
}

Output

 Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
 Triangle Area : 151.89535030326

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
 Triangle Area : 77.5080666366411

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
 Triangle Area : 84.134891691854
<?php
/*
  Php program
  Calculate area of triangle
*/
class TriangleArea
{
	//Calculate area of triangle by given three side
	public 	function triangle_area($a, $b, $c)
	{
		$side = ($a + $b + $c) / 2;
		//Calculate area of triangle
		$area = sqrt($side *(($side - $a) *($side - $b) *($side - $c)));
		//Display the result of triangle area
		echo(" Triangle Sides [ a : ". $a .", b : ". $b ." c : ". $c ." ] ");
		echo("\n Triangle Area : ". $area ."\n\n");
	}
}

function main()
{
	$obj = new TriangleArea();
	/*

			  24.2        .
			    ③      .   . ②
			        .        .   13.1 
			    .              .
			  -------------------       
			        ①
			           24
			*/
	//Test Cases  
	$obj->triangle_area(24, 13.1, 24.2);
	$obj->triangle_area(22.2, 20.2, 7.7);
	$obj->triangle_area(12.8, 13.7, 15.9);
}
main();

Output

 Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
 Triangle Area : 151.89535030326

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
 Triangle Area : 77.508066636641

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
 Triangle Area : 84.134891691854
/*
  Node Js program
  Calculate area of triangle
*/
class TriangleArea
{
	//Calculate area of triangle by given three side
	triangle_area(a, b, c)
	{
		var side = (a + b + c) / 2;
		//Calculate area of triangle
		var area = Math.sqrt(side *((side - a) *(side - b) *(side - c)));
		//Display the result of triangle area
		process.stdout.write(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		process.stdout.write("\n Triangle Area : " + area + "\n\n");
	}
}

function main(args)
{
	var obj = new TriangleArea();
	/*

			  24.2        .
			    ③      .   . ②
			        .        .   13.1 
			    .              .
			  -------------------       
			        ①
			           24
			*/
	//Test Cases  
	obj.triangle_area(24, 13.1, 24.2);
	obj.triangle_area(22.2, 20.2, 7.7);
	obj.triangle_area(12.8, 13.7, 15.9);
}
main();

Output

 Triangle Sides [ a : 24, b : 13.1 c : 24.2 ]
 Triangle Area : 151.89535030325973

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
 Triangle Area : 77.50806663664115

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
 Triangle Area : 84.13489169185397
#   Python 3 program
#   Calculate area of triangle
import math
class TriangleArea :
	# Calculate area of triangle by given three side
	def triangle_area(self, a, b, c) :
		side = (a + b + c) / 2
		# Calculate area of triangle
		area = math.sqrt(side * ((side - a) * (side - b) * (side - c)))
		
		# Display the result of triangle area
		print(" Triangle Sides [ a : ", a ,", b : ", b ," c : ", c ," ] ", end = "")
		print("\n Triangle Area : ", area ,"\n\n", end = "")
	

def main() :
	obj = TriangleArea()
	# Test Cases  
	# 
	# 		  24.2        .
	# 		    ③      .   . ②
	# 		        .        .   13.1 
	# 		    .              .
	# 		  -------------------       
	# 		        ①
	# 		           24
	# 		
	
	obj.triangle_area(24, 13.1, 24.2)
	obj.triangle_area(22.2, 20.2, 7.7)
	obj.triangle_area(12.8, 13.7, 15.9)


if __name__ == "__main__": main()

Output

 Triangle Sides [ a :  24 , b :  13.1  c :  24.2  ]
 Triangle Area :  151.89535030325973

 Triangle Sides [ a :  22.2 , b :  20.2  c :  7.7  ]
 Triangle Area :  77.50806663664115

 Triangle Sides [ a :  12.8 , b :  13.7  c :  15.9  ]
 Triangle Area :  84.13489169185397
#   Ruby program
#   Calculate area of triangle

class TriangleArea

	# Calculate area of triangle by given three side
	def triangle_area(a, b, c)
	
		side = (a + b + c) / 2
		# Calculate area of triangle
		area = Math.sqrt(side * ((side - a) * (side - b) * (side - c)))
		# Display the result of triangle area
		print(" Triangle Sides [ a  : ", a ,", b  : ", b ," c  : ", c ," ] ")
		print("\n Triangle Area  : ", area ,"\n\n")
	end
end
def main()

	obj = TriangleArea.new()
	# Test Cases  
	# 
	# 		  24.2        .
	# 		    ③      .   . ②
	# 		        .        .   13.1 
	# 		    .              .
	# 		  -------------------       
	# 		        ①
	# 		           24
	# 		
	
	obj.triangle_area(24, 13.1, 24.2)
	obj.triangle_area(22.2, 20.2, 7.7)
	obj.triangle_area(12.8, 13.7, 15.9)
end
main()

Output

 Triangle Sides [ a  : 24, b  : 13.1 c  : 24.2 ] 
 Triangle Area  : 151.89535030325973

 Triangle Sides [ a  : 22.2, b  : 20.2 c  : 7.7 ] 
 Triangle Area  : 77.50806663664115

 Triangle Sides [ a  : 12.8, b  : 13.7 c  : 15.9 ] 
 Triangle Area  : 84.13489169185397

/*
  Scala program
  Calculate area of triangle
*/
class TriangleArea
{
	//Calculate area of triangle by given three side
	def triangle_area(a: Double, b: Double, c: Double): Unit = {
		var side: Double = (a + b + c) / 2;
		//Calculate area of triangle
		var area: Double = Math.sqrt(side * ((side - a) * (side - b) * (side - c)));
		//Display the result of triangle area
		print(" Triangle Sides [ a : " + a + ", b : " + b + " c : " + c + " ] ");
		print("\n Triangle Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: TriangleArea = new TriangleArea();
		/*

				  24.2        .
				    ③      .   . ②
				        .        .   13.1 
				    .              .
				  -------------------       
				        ①
				           24
				*/
		//Test Cases  
		obj.triangle_area(24, 13.1, 24.2);
		obj.triangle_area(22.2, 20.2, 7.7);
		obj.triangle_area(12.8, 13.7, 15.9);
	}
}

Output

 Triangle Sides [ a : 24.0, b : 13.1 c : 24.2 ]
 Triangle Area : 151.89535030325973

 Triangle Sides [ a : 22.2, b : 20.2 c : 7.7 ]
 Triangle Area : 77.50806663664115

 Triangle Sides [ a : 12.8, b : 13.7 c : 15.9 ]
 Triangle Area : 84.13489169185397
/*
  Swift program
  Calculate area of triangle
*/
import Foundation
class TriangleArea
{
	//Calculate area of triangle by given three side
	func triangle_area(_ a: Double, _ b: Double, _ c: Double)
	{
		let side: Double = (a + b + c) / 2;
		//Calculate area of triangle
		let area: Double = sqrt(side * ((side - a) * (side - b) * (side - c)));
		
			//Display the result of triangle area
		print(" Triangle Sides [ a : ", a ,", b : ", b ," c : ", c ," ] ", terminator: "");
		print("\n Triangle Area : ", area ,"\n\n", terminator: "");
	}
}
func main()
{
	let obj: TriangleArea = TriangleArea();
	/*

			  24.2        .
			    ③      .   . ②
			        .        .   13.1 
			    .              .
			  -------------------       
			        ①
			           24
			*/
	//Test Cases  
	obj.triangle_area(24, 13.1, 24.2);
	obj.triangle_area(22.2, 20.2, 7.7);
	obj.triangle_area(12.8, 13.7, 15.9);
}
main();

Output

 Triangle Sides [ a :  24.0 , b :  13.1  c :  24.2  ]
 Triangle Area :  151.89535030326

 Triangle Sides [ a :  22.2 , b :  20.2  c :  7.7  ]
 Triangle Area :  77.5080666366411

 Triangle Sides [ a :  12.8 , b :  13.7  c :  15.9  ]
 Triangle Area :  84.134891691854


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







© 2021, kalkicode.com, All rights reserved