Logical Operator in C language
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 15;
// Logical AND operator (&&)
if (a > 0 && b > 0) {
printf("Both a and b are greater than 0\n");
} else {
printf("At least one of a or b is not greater than 0\n");
}
// Logical OR operator (||)
if (b > 10 || c > 10) {
printf("Either b or c is greater than 10\n");
} else {
printf("Neither b nor c is greater than 10\n");
}
// Logical NOT operator (!)
if (!(a == b)) {
printf("a is not equal to b\n");
} else {
printf("a is equal to b\n");
}
return 0;
}
Explanation:
1. Declare three integer variables a
, b
, and c
with values 5, 10, and 15.
2. Use the Logical AND operator (&&
) to check if both a
and b
are greater than 0. Print a message accordingly.
3. Use the Logical OR operator (||
) to check if either b
or c
is greater than 10. Print a message accordingly.
4. Use the Logical NOT operator (!
) to check if a
is not equal to b
. Print a message accordingly.