[Question]
  [
**Closed**. This question needs [details or clarity](/help/closed-questions). It is not currently accepting answers.
 
 
---
**Want to improve this question?** Add details and clarify the problem by [editing this post](/posts/11020/edit).
Closed 2 years ago.
[Improve this question](/posts/11020/edit) 
Given three sides of a triangle, print area of this triangle.
Test cases:
**In:** `2,3,4`
**Out:** `2.90473750965556`
**In:** `3,4,5`
**Out:** `6`
Given the sides \$a\$, \$b\$, and \$c\$, you can assume that \$a>0\$, \$b>0\$, \$c>0\$, \$a+b>c\$, \$b+c>a\$, and \$c+a>b\$.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), shortest answer per language in bytes wins.
  
[Answer]
## J, ~~23~~ 19 chars
```
   (4%~2%:[:*/+/-0,+:)
   (4%~2%:[:*/+/-0,+:) 2 3 4
2.90474
   (4%~2%:[:*/+/-0,+:) 3,4,5
6
```
17-char version if input is in `i`: `4%~%:*/(+/,+/-+:)i`
original 23-char version: `(%:@(+/**/@(+/-+:))%4:)`
[Answer]
## APL ~~21~~ 20
Takes screen input via ←⎕
```
(×/(+/t÷2)-0,t←⎕)*.5
```
[Answer]
**Mathematica 23**
```
√Times@@(+##/2-{0,##})&
```
[Answer]
# Python 2, 53
```
t=input()
s=a=sum(t)/2.
for x in t:a*=s-x
print a**.5
```
Input: `2,3,4`
Output: `2.90473750966`
[Answer]
## Python 57 bytes
```
a,b,c=input()
s=(a+b+c)*.5
print(s*(s-a)*(s-b)*(s-c))**.5
```
Using [Heron's Formula](http://mathworld.wolfram.com/HeronsFormula.html).
Sample usage:
```
$ echo 2,3,4 | python triangle-area.py
2.90473750966
$ echo 3,4,5 | python triangle-area.py
6.0
```
---
**A 58 byte variant:**
```
a,b,c=input()
print((a+b+c)*(b+c-a)*(a+c-b)*(a+b-c))**.5/4
```
[Answer]
### GolfScript, 38 characters
```
~].~++:d\{2*d\-*}/'"#{'\+'**0.5/4}"'+~
```
Since the question didn't specify otherwise I chose to work only on integer lengths. Sides must be given on STDIN separated by spaces.
Example:
```
> 2 3 4
2.9047375096555625
```
[Answer]
## R : ~~48~~ 43 characters
```
f=function(...)prod(sum(...)/2-c(0,...))^.5
```
Using Heron's formula as well but taking advantage of R's vectorization.  
Thanks to @flodel for the idea of the ellipsis.
Usage:
```
f(2,3,4)
[1] 2.904738
f(3,4,5)
[1] 6
```
[Answer]
# Mathematica ~~20~~ 16 or ~~22~~ 18 bytes
With 4 bytes saved by @swish.
This returns an exact answer:
```
Area@SSSTriangle@
```
**Example**
```
Area@SSSTriangle[2,3,4]
```
[![image](https://i.stack.imgur.com/Gn8F1.png)](https://i.stack.imgur.com/Gn8F1.png)
---
To return the answer in decimal form, two additional bytes are required.
```
N@Area@SSSTriangle[2,3,4]
```
2.90474
[Answer]
# K, 23
```
{sqrt s**/(s:.5*+/x)-x}
```
Example
```
k){sqrt s**/(s:.5*+/x)-x} 2 3 4
2.904738
```
[Answer]
### APL, 23 20 characters
```
{(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4
```
Example:
```
> {(×/(+/⍵÷2)-0,⍵)*÷2} 2 3 4
2.90474
```
[Answer]
## Javascript, ~~88~~ 85
```
v=prompt().split(/,/g);s=v[0]/2+v[1]/2+v[2]/2;Math.sqrt(s*(s-v[0])*(s-v[1])*(s-v[2]))
```
Not good but fun :) Also Heron...
Demonstrates the ungolfability of simple problems in JS lol
**Note**: run from console to see result.
88->85: Removed `a`, `b` and `c`.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 55 bytes
```
#define f(a,b,c)sqrt((a+b+c)*(a+b-c)*(a-b+c)*(b+c-a))/4
```
[Try it online!](https://tio.run/##S9ZNT07@/185JTUtMy9VIU0jUSdJJ1mzuLCoREMjUTtJO1lTC0TrgmldCB9I6iZqauqb/M9NzMzT0KzmUigoyswrSdNQUk2LyVPSSdMw0jHWMdHUtMaUAYrrmIJkav//S07LSUwv/q@bkwsA "C (gcc) – Try It Online")
Yet another implementation of Hero's formula.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
SH;_P½ʋ
```
[Try it online!](https://tio.run/##y0rNyan8/z/Ywzo@4NDeU93/D7c/alrz/390tJGOsY5JrE40kNQxjY0FAA "Jelly – Try It Online")
[Answer]
# Haskell: 51 (27) characters
```
readLn>>=(\l->print$sqrt$product$map(sum l/2-)$0:l)
```
A very straight-forward implementation of Heron's formula. Example run:
```
Prelude> readLn>>=(\l->print$sqrt$product$map(sum l/2-)$0:l)
[2,3,4]
2.9047375096555625
Prelude>
```
Note that it accepts any numeric input, not only integers. And if the input already is in l the solution only needs to be 36 characters long, and if we are not interested in printing the answer the solution only needs to be 30 characters long. What more is that if we can allow ourself to change the input format we can remove 3 more characters. So if our input looks like [2,3,4,0.0] and is already in l we can get our answer with only:
```
sqrt$product$map(sum l/2-)l
```
Example run:
```
Prelude> let l = [2,3,4,0.0]
Prelude> sqrt$product$map(sum l/2-)l
2.9047375096555625
Prelude>
```
[Answer]
# PHP, ~~78~~ 77
```
<?=sqrt(($s=array_sum($c=fgetcsv(STDIN))/2)*($s-$c[0])*($s-$c[1])*$s-=$c[2]);
```
Useage:
```
php triangle.php
2,3,4
```
Output: `2.9047375096556`
I don't think I can make it shorter? I'm still new to golfing. Anyone let me know if I overlooked something.
Thanks Primo for saving me 1 byte, lol.
[Answer]
## JavaScript (84 86)
```
s=(eval('abc '.split('').join('=prompt()|0;'))+a+b)/2;Math.sqrt(s*(s-a)*(s-b)*(s-c))
```
Another JavaScript solution based on Heron's formula, but trying a different approach for loading variables. Needs to be run from the console. Each side is entered in a separate prompt.
**EDIT**: Make use of return value of `eval` to save 2 characters. Beats @tomsmeding, wahoo! :)
[Answer]
# Excel, 42 bytes
Based on Heron's formula, high school algebra to golf.
```
=SQRT(((A1+B1)^2-C1^2)*(C1^2-(A1-B1)^2))/4
```
Ungolfed / Unalgebra'ed
```
=SQRT((A1+B1+C1)/2*(((A1+B1+C1)/2)-A1)*(((A1+B1+C1)/2)-B1)*(((A1+B1+C1)/2)-C1))
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt/), ~~17~~ ~~16~~ 15 bytes
```
½*Nx
NmnU ×*U q
```
[Test it](http://ethproductions.github.io/japt/?v=1.4.5&code=vSpOeApObW5VINcqVSBx&input=MiwzLDQ=)
Saved 2 bytes thanks to [ETH](https://codegolf.stackexchange.com/users/42545/ethproductions) pointing out a redundant newline and some alternative ways to reduce the array.
[Answer]
# Tcl, 74 chars.
```
proc R {a b c} {set s ($a+$b+$c)/2.
expr sqrt($s*($s-$a)*($s-$b)*($s-$c))}
```
Pass the sides as argument.
For the input `2 3 4` the value of `s` is `(2+3+4)/2.` as string. Double evaluation FTW.
[Answer]
## Julia 0.6.0, 48 bytes
Basically heron's formula:
```
f(a,b,c)=(p=(a+b+c)/2;sqrt(p*(p-a)*(p-b)*(p-c)))
```
[Answer]
# TI-BASIC, ~~14~~ 12 bytes
```
4⁻¹√(sum(Ansprod(sum(Ans)-2Ans
```
Starting from a Heron's Formula routine [written by Kenneth Hammond (Weregoose)](http://adriweb.free.fr/upload/ti/weregoose.html), I golfed off two bytes. Note that TI-BASIC is tokenized, and each token, like `Ans` and `prod(`, is one or two bytes in the calculator's memory.
Input through `Ans` i.e. in the form `{a,b,c}:[program name]`.
Explained:
```
                   sum(Ans)-2*Ans   (a+b+c)-2{a,b,c}={b+c-a,c+a-b,a+b-c}
          Ans*prod(                 {a,b,c}*(b+c-a)(c+a-b)(a+b-c)
      sum(                          (a+b+c)(b+c-a)(c+a-b)(a+b-c)
4⁻¹*√(                              √((a+b+c)(b+c-a)(c+a-b)(a+b-c)/16)
                                    =√(s(s-a)(s-b)(s-c))
```
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 34 bytes
```
9ksssddlsld++2/d3R-rdls-rdld-***vp
```
[Try it online!](https://tio.run/##S0n@b6RgrGCi8N8yu7i4OCUlpzgnRVvbSD/FOEi3CMgDESm6WlpaZQX//wMA "dc – Try It Online")
[Answer]
# [ARBLE](https://github.com/TehFlaminTaco/ARBLE), 36 bytes
```
sqrt(a*(a-b)*(a-c)*(a-d))((b+c+d)/2)
```
Heron's formula.
[Try it online!](https://tio.run/##SyxKykn9/7@4sKhEI1FLI1E3SRNEJoPJFE1NDY0k7WTtFE19I83///8bmfw3NvhvaAEA "ARBLE – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-MList::Util=sum -ap`, 40 bytes
```
$r=$t=.5*sum@F;map$r*=$t-$_,@F;$_=sqrt$r
```
[Try it online!](https://tio.run/##K0gtyjH9/1@lyFalxFbPVKu4NNfBzTo3sUClSAsopKsSrwPkq8TbFhcWlagU/f9vrGCiYPovv6AkMz@v@L@ur6megaEBkPbJLC6xsgotycyxBZrxXzexAAA "Perl 5 – Try It Online")
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 10 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
╝0∞♀»♦▓y╩╪
```
[Run and debug it](https://staxlang.xyz/#p=bc30ec0caf04b279cad8&i=[2.0,+3.0,+4.0]%0A[3.0,+4.0,+5.0]&a=1&m=2)
Operates triples of floating point numbers. Uses [Heron's Formula](https://en.wikipedia.org/wiki/Heron%27s_formula)
[Answer]
# APL(NARS), 16 chars, 32 bytes
```
{√×/(+/⍵÷2)-0,⍵}
```
One has to cenvert the Erone formula if a, b, c are sides of triangle
```
p   =(a+b+c)/2 
Area=√p*(p-a)(p-b)(p-c)
```
to APL language... test
```
  f←{√×/(+/⍵÷2)-0,⍵}
  f 2 3 4
2.90473751
  f 3 4 5
6
```
]