Getting '0' as the result for area of a triangle in C

Viewed 24

When I use the below code, it's execution results in '0':

#include<stdio.h>
int main(){
    float base;
    printf("enter the base");
    scanf("%f", &base);

    float height;
    printf("enter the height");
    scanf("%f", &height);

    float area = 1/2 * base * height;
    printf("Area of the triangle is :%f", area);
    return 0;
}

But, if I tweak it a little bit (i.e., give area = base * height * 1/2 instead of 1/2 * base * height)..and execute it, I get the right answer:

Am I doing something wrong here? If not, why does this happen?

output (please refer if you'd like to)

0 Answers
Related