Calculation to determine if triangle is rectangle does not give expected result

2

The program I created receives three integer values, calculates the values inside% cos_de% and if the condition is correct it should show the SIM (the triangle is a rectangle), and if the condition is not satisfied, it should show NO.

By typing values 3, 4 and 5 the output must be YES, typing 3, 3 and 4, the output should be NAO, but this is giving YES in all cases.

Follow the code:

#include <stdio.h>
#include <math.h>

int main (void)

{
    int hip, cat1, cat2;

    scanf("%d", &cat1);

    scanf("%d", &cat2);

    scanf("%d", &hip);

    if (hip = pow(cat1,2) + pow(cat2,2))
    {
        printf("SIM");
    }

    else
    {
        printf("NAO");
    }


    return 0;
}

How to solve?

    
asked by anonymous 04.04.2015 / 01:30

2 answers

4

There are two problems:

  • You used = and not == , to compare is the second, you assigned a new value to the hypotenuse.
  • You did not use the square root, so the formula is wrong. But there is a better formula.
  • See:

    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
        int hip, cat1, cat2;
        scanf("%d", &cat1);
        scanf("%d", &cat2);
        scanf("%d", &hip);
        if (hip*hip == cat1*cat1 + cat2*cat2) {
            printf("SIM");
        } else {
            printf("NAO");
        }
        return 0;
    }
    

    See running on ideone .

        
    04.04.2015 / 01:40
    2

    Previous version variant (@bigown)

    #include <stdio.h>
    #include <math.h>
    #define ERROMAX 0.0001
    
    int main (void) {
        float hip, cat1, cat2;
        scanf("%f", &cat1);
        scanf("%f", &cat2);
        scanf("%f", &hip);
        if (fabsf(hip - sqrt(pow(cat1,2) + pow(cat2,2))) < ERROMAX ) {
            printf("SIM\n");
        } else {
            printf("NAO\n");
        }
        return 0;
    }
    

    so as to give

    1 1 1 -> não 
    
    3 4 5 -> sim
    
        
    04.04.2015 / 13:10