Java Program To Demonstrate Logical Operators

Write Java program to demonstrate the use of logical operators

// WAP to demonstrate logical operator

public class LogicalOperator
{
    public static void main(String[] args)
    {
        int a = 7, b = 0, c = -3;

        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);

        
        //logical and
        System.out.println("(a<b)&&(a<c) :"+((a<b)&&(a<c)));
        System.out.println("(b<a)&&(b<c) :"+((b<a)&&(b<c)));
        System.out.println("(b<a)&&(b<c) :"+((c<a)&&(c<b)));
        System.out.println("(a == b) && (c > b) :"+((a == b) && (c > b)));
        System.out.println("(a == b) && (c < b) :"+((a == b) && (c < b)));
        System.out.println();

        //logical or
        System.out.println("(a<b)||(a<c) :"+((a<b)||(a<c)));
        System.out.println("(b<a)||(b<c) :"+((b<a)||(b<c)));
        System.out.println("(b<a)||(b<c) :"+((c<a)||(c<b)));
        System.out.println("(a == b) || (c < b) :"+((a == b) || (c < b)));
        System.out.println("(a != b) || (c < b) :"+((a != b) || (c < b)));
        System.out.println();

        //logical not
        System.out.println("(a!=b) :"+(a!=b));
        System.out.println("(a!=c) :"+(a!=c));
        System.out.println("(b!=c) :"+(b!=c));
        System.out.println("!(a != b) :"+!(a != b));
        System.out.println("!(a == b) :"+!(a == b));
        System.out.println();


        System.out.println("(a != b) || (c < b) && (a < c) :"+((a != b) || (c < b) && (a < c)));
        System.out.println("(a == b) || (c < b) && (a > c) :"+((a == b) || (c < b) && (a > c)));
        System.out.println("(a == b) && (c < b) || (a > c) :"+((a == b) && (c < b) || (a > c)));
       
        }
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.