C Program to Demonstrate Logical Operators

C Program to Demonstrate Logical Operators

Write C Code to demonstrate the use of logical operators

//Write a c program to demonstrate logical operators

#include <stdio.h>

int main()
{
    int a = 7, b = 0, c = -3, ans;

    printf("a = %d", a);
    printf("\nb = %d", b);
    printf("\nc = %d\n\n", c);

    ans = a && b;      //logical and
    printf("a && b : %d \n", ans);

    ans = a && c;      //logical and
    printf("a && c : %d \n", ans);

    ans = a || b;      //logical or
    printf("a || b : %d \n", ans);

    ans = a || c;      //logical or
    printf("a || c : %d \n", ans);

    ans = !a;      //logical not
    printf("!a : %d \n", ans);

    ans = !b;      //logical not
    printf("!b : %d \n", ans);

    ans = !(a != b);				//logical not
    printf("!(a != b) is : %d \n", ans);

    ans = !(a == b);
    printf("!(a == b) is : %d \n", ans);

    ans = (a == b) && (c > b);      //logical and
    printf("(a == b) && (c > b) is : %d \n", ans);

    ans = (a == b) && (c < b);
    printf("(a == b) && (c < b) is : %d \n", ans);

    ans = (a == b) || (c < b);		//logical or
    printf("(a == b) || (c < b) is : %d \n", ans);

    ans = (a != b) || (c < b);
    printf("(a != b) || (c < b) is : %d \n", ans);

    ans = (a != b) || (c < b) && (a < c);
    printf("(a != b) || (c < b) && (a < c) is : %d \n", ans);

    ans = (a == b) || (c < b) && (a > c);
    printf("(a == b) || (c < b) && (a > c) is : %d \n", ans);

    ans = (a == b) && (c < b) || (a > c);
    printf("(a == b) && (c < b) || (a > c) is : %d \n", ans);

    return 0;
}

Output:

a = 7
b = 0
c = -3

a && b : 0
a && c : 1
a || b : 1
a || c : 1
!a : 0
!b : 1
!(a != b) is : 0
!(a == b) is : 1
(a == b) && (c > b) is : 0
(a == b) && (c < b) is : 0
(a == b) || (c < b) is : 1
(a != b) || (c < b) is : 1
(a != b) || (c < b) && (a < c) is : 1
(a == b) || (c < b) && (a > c) is : 1
(a == b) && (c < b) || (a > c) is : 1