C++ Program To Demonstrate Logical Operators

Write C++ Code to demonstrate the use of logical operators

// Write C++ program to demonstrate comparison operator

#include <bits/stdc++.h>

using namespace std;

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

    cout << "a = " << a <<endl ;
    cout << "b = " << b <<endl ;
    cout <<"c = " << c <<endl<<endl ;

    ans = a && b;      //logical and
    cout << "a && b : " << ans <<endl;

    ans = a && c;      //logical and
    cout << "a && c :" << ans <<endl;

    ans = a || b;      //logical or
    cout << "a || b :"<< ans<<endl;

    ans = a || c;      //logical or
    cout << "a || c :" << ans <<endl ;

    ans = !a;      //logical not
    cout << "!a : " << ans <<endl;

    ans = !b;      //logical not
    cout << "!b :" <<ans <<endl;

    ans = !(a != b);                    //logical not
    cout << "!(a != b) is :" << ans <<endl;

    ans = !(a == b);
    cout << "!(a == b) is : "<< ans << endl;

    ans = (a == b) && (c > b);      //logical and
    cout << "(a == b) && (c > b) is :"<< ans <<endl ;

    ans = (a == b) && (c < b);
    cout << "(a == b) && (c < b) is : " << ans << endl ;
    ans = (a == b) || (c < b);          //logical or
    cout << "(a == b) || (c < b) is :" <<ans  << endl;

    ans = (a != b) || (c < b);
    cout<< "(a != b) || (c < b) is : " << ans << endl;

    ans = (a != b) || (c < b) && (a < c);
    cout <<"(a != b) || (c < b) && (a < c) is :" << ans <<endl;

    ans = (a == b) || (c < b) && (a > c);
    cout << "(a == b) || (c < b) && (a > c) is : " << ans <<endl;

    ans = (a == b) && (c < b) || (a > c);
    cout << "(a == b) && (c < b) || (a > c) is : " << ans<<endl;
    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

<< Previous | Next >>