# Thread: New to Programming - Simple Math Program Issue

1. ## New to Programming - Simple Math Program Issue

Hello! I am new here and to programming as a whole. I am using GCC on Fedora to do a summer semeseter in C Programming. I am currently learning "if statements" while doing a program that calculates the area of a triangle. My problem is that the result is always "0". I am pretty sure the problem lies within my math, but I can't figure out how I have set it up wrong. Thanks for reading/looking.

I am using Heron's Formula. Here it is for reference:

area=sqrt(s(s-a)(s-b)(s-c)) where s=(a+b+c)/2

Code:
```#include <stdio.h>
#include <math.h>

int main ()
{

float area, sideA, sideB, sideC, halfP;

printf("\n This program will calculate the area of a triangle.");
printf("Please enter the value for the first side: ");
scanf("%f", &sideA);
printf("Please enter the value for the second side: ");
scanf("%f", &sideB);
printf("Please enter the value for the third side: ");
scanf("%f", &sideC);

if (0 > sideA)
printf("The distance must be positive. \n");
else if (0 > sideB)
printf("The distance must be positive. \n");
else if (0 > sideC)
printf("The distance must be positive. \n");
else
halfP = (sideA+sideB +sideC)/2;
area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
printf("\n The area of the triangle is %f \n", &area);

return 0;
}```

2. The last printf() should not have an & before area:

Code:
```/* ... */
printf("\n The area of the triangle is %f \n", area);```
Also the last else needs braces so that it applies to the next 3 lines:
Code:
```/* ... */
else{
halfP = (sideA+sideB+sideC)/2;
area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
printf("\n The area of the triangle is %f \n", area);
}```

3. It actually worked fine once I removed the "&" from the last printf line on area. I am a noobie, but I don't think you need the brackets if the "else" line is part of a if-else chain. When I tried adding brackets it just gave me errors when I compiled. When I left them off again it worked fine.

EDIT: Ok, I see what you were saying about adding brackets to just the "else" part. That made it to where if someoen entered a number <0 it didn't continue to try and compute and print the result. Thanks!

[cameron@localhost Program3]\$ ./Exercise6

This program will calculate the area of a triangle.Please enter the value for the first side: 5
Please enter the value for the second side: 5
Please enter the value for the third side: 5

The area of the triangle is 10.825317
A bit sloppy looking, but I will clean it up now that it is working haha.

4. This isn't needed, but you could combine the 0 > number checks and eliminate the need for that final else:

Code:
```/* ... */
if(0 > sideA || 0 > sideB || 0 > sideC){
printf("The distance must be positive. \n");
return 0;
}

halfP = (sideA+sideB +sideC)/2;
area = sqrt(halfP*(halfP-sideA)*(halfP-sideB)*(halfP-sideC));
printf("\n The area of the triangle is %f \n", area);```

5. Ah HA!, I didn't think about using hte "or" statement. I was tapping my head trying to think of how to put them all on one "if" statement. Thanks again!