Archive:Area of triangle using C: Difference between revisions
Appearance
wikademia>JWSchmidt category |
wikademia>Mickraus Now portable; reformatted for readability; correct definition of main |
||
| Line 1: | Line 1: | ||
<pre> | <pre> | ||
#include<stdio.h> | #include<stdio.h> | ||
#include<math.h> | #include<math.h> | ||
/* main: calculate the area of a triangle using Heron's Formula */ | |||
int main(void) | |||
{ | { | ||
float a,b,c,s,area; | float a, b, c, s, area; | ||
printf("Enter sides of triangle: "); | |||
printf(" | scanf("%f%f%f", &a, &b, &c); | ||
scanf("%f%f%f",&a,&b,&c); | s = (a + b + c) / 2; | ||
s=(a+b+c) /2; | |||
area=sqrt(s*(s-a)*(s-b)*(s-c)); | /* no error checking: sqrt() works only if its arg >= 0 */ | ||
printf(" | area = sqrt(s * (s-a) * (s-b) * (s-c)); | ||
printf("Area = %f\n", area); | |||
/* indicate success */ | |||
return 0; | |||
} | } | ||
</pre> | </pre> | ||
[[Category:C source code examples]] | [[Category:C source code examples]] | ||
Revision as of 15:14, 30 January 2007
#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;
}