PAPAmidNIHGT PAPAmidNIHGT - 1 month ago 6
C Question

C eror in comparing 3 numbers (1 of them is negative)

it would be nice if someone would help me with my code .
According to task i have a few numbers (at least 1 negative) in case i`m getting 3 numbers > 0 or = 0 then answer is 0 .Cod should find the closest to 0 number. Here is my cod but it is not working .

#include <stdio.h>
#include <conio.h>
int main()
{
int uno, dos, tres; //numbers


printf("enter your numbers");
scanf_s("%d %d %d", &uno, &dos, &tres);
if (uno > 0 || uno == 0 && dos > 0 || dos == 0 && tres > 0 || tres == 0)
printf("There is no negative numbers ...");
else if (uno == 0 || uno > 0 && dos > 0 || dos == 0 && tres < 0)
printf("Your number is %d", &tres);
else if (uno > 0 || uno == 0 && dos < 0 && tres < 0)
if (dos < tres || tres == dos)
printf("Your number is %d", &tres);
else
printf("Your number is %d", &dos);
else if (uno < 0 && dos < 0 && tres < 0)
if (uno < dos && uno < tres)
if (dos < tres || tres == dos)
printf("Your number is %d", &tres);
else printf("Your number is %d", dos);
else if (dos < uno && dos < tres)
if (uno < tres|| uno==tres)
printf("Your number is %d",& tres);
else printf("Your number is %d", &uno);
else if (tres < uno && tres < dos)
if (uno < dos || uno==dos)
printf("Your number is %d",&dos);
else printf("Your number id %d",&uno);

_getch();
return 0;
}


Appreciate any help.

Answer

Logical OR (||) has greater precedence than logical AND (&&) so you should add some parenthesis. eg.

if (uno > 0 || uno == 0 && dos > 0 || dos == 0 && tres > 0 || tres == 0)

should become

if ((uno > 0 || uno == 0) && (dos > 0 || dos == 0) && (tres > 0 || tres == 0))
Comments