Wednesday, 27 January 2016

Write C++ Program to demonstrate the working of Logical Operators.

Leave a Comment
Questions:


Write C++ Program to demonstrate the working of Logical Operators.

Explanation:
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.


Code:
/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/

#include <iostream>
using namespace std;
int main()
{
    int a = 5;
    int b = 20;
    int c; 
    if (a && b)
    {
        cout << "Line 1 - Condition is true\n";
    }
    if (a || b)
    {
        cout << "Line 2 - Condition is true\n";
    }
    /* lets change the value of  a and b */
    a = 0;
    b = 10; 
    if (a && b)
    {
        cout << "Line 3 - Condition is true\n";
    }
    else
    {
        cout << "Line 3 - Condition is not true\n";
    }
    if (!(a && b))
    {
        cout << "Line 4 - Condition is true\n";
    }
}

Output:

Write C++ Program to demonstrate the working of Logical Operators.
If You Enjoyed This, Take 5 Seconds To Share It