Archive:Area of triangle using C: Difference between revisions
Appearance
wikademia>Mickraus Now portable; reformatted for readability; correct definition of main |
wikademia>Wikademia |
||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
< | <source lang="c"> | ||
#include<stdio.h> | #include<stdio.h> | ||
#include<math.h> | #include<math.h> | ||
| Line 18: | Line 18: | ||
return 0; | return 0; | ||
} | } | ||
</ | </source> | ||
[[Category:C source code examples]] | [[Category:C source code examples]] | ||
Latest revision as of 07:54, 12 June 2009
<source lang="c">
- include<stdio.h>
- include<math.h>
/* main: calculate the area of a triangle using Heron's Formula */ int main(void) {
float a, b, c, s, area;
printf("Enter sides of triangle: ");
scanf("%f%f%f", &a, &b, &c);
s = (a + b + c) / 2;
/* no error checking: sqrt() works only if its arg >= 0 */
area = sqrt(s * (s-a) * (s-b) * (s-c));
printf("Area = %f\n", area);
/* indicate success */ return 0;
} </source>