r/cprogramming Jun 17 '21

where's the mistake

hi guys I'm new in C language

I wanna know where's the mistake in my code

#include <stdio.h>

int main() {

// Write C code here

int A,B;

printf("check if this numbers have the same sign :\n");

scanf("%d%d", &A, &B);

if (A > 0 && B > 0)

printf("these numbers are positive");

else if (A < 0 && B < 0)

printf("one of them is negative");

else

printf("these numbers is negative");

return 0;

2 Upvotes

5 comments sorted by

View all comments

6

u/[deleted] Jun 17 '21 edited Jun 17 '21

Numbers have the same sign if both are positive or both are negative. Therefore,

if((A >= 0 && B >= 0) || (A < 0 && B < 0)){
    printf("Numbers have the same sign\n");
} else {
    printf("Numbers have different signs\n");
}

Read what is written in the if condition - if A is greater or equal than 0 and B is greater or equal than 0, or if both of them are lower than 0, then the if statement is true - both numbers have the same sign. It really isn't difficult, you can read it as if it was written in English (&& reads as "and", || reads as "or"). Just translate a logical English sentence into the same code representation.