Jump to content

Archive:Area of triangle using C: Difference between revisions

From IdeaWazaWiki
wikademia>Mickraus
Now portable; reformatted for readability; correct definition of main
wikademia>MichaelFrey
mNo edit summary
Line 1: Line 1:
<pre>
<source lang="c">
#include<stdio.h>
#include<stdio.h>
#include<math.h>
#include<math.h>
Line 18: Line 18:
     return 0;
     return 0;
}
}
</pre>
</source>


[[Category:C source code examples]]
[[Category:C source code examples]]

Revision as of 06:39, 17 May 2007

<source lang="c">

  1. include<stdio.h>
  2. 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>