Question:
The program I created receives three integer values, calculates these values within the if
and if the condition is correct it should show the YES on the screen (the triangle is a rectangle), and if the condition is not satisfied, it should show NO.
By typing the values 3, 4 and 5 the output must be YES, by typing 3, 3 and 4, the output should be NO, but it is YES in all cases.
Here's 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?
Answer:
There are two problems:
- You used
=
and not==
, to compare is the second, you assigned a new value to the hypotenuse. - You didn't use the square root, so the formula is wrong. But there is a better formula.
Look:
#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");
}
See it working on ideone . And on repl.it. I also put it on GitHub for future reference . Note that I did it a little differently there.