4
The program I created takes three integer values, calculates within the if
these values and if the condition is correct it should show on the screen the YES (the triangle is rectangle), and if the condition is not satisfied, it should show NO.
Typing the values 3, 4 and 5 the output has to be YES, typing 3, 3 and 4, the output should be NO, but is giving YES in all cases.
Follows 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?
use == instead of =, and also to be the formula of the rectangular triangle must have root.
– Rafael Bluhm
Turn on your compiler warnings and fix them all before accepting the executable.
– pmg
If your inputs are integer, a simpler solution like those of the bigown response will always bring correct results. Only when one admits floating point inputs (which goes beyond the question, but I am quoting by completeza) is that an approximate solution is necessary - in which case Jjoao’s answer demonstrates the most usual way of dealing with the problem (establish an error margin for FP comparisons). Here it is an example which helps to prove the correctness of the methods presented.
– mgibsonbr