NOR (Logical Operator) - Explained with Programming Examples


NOR (Logical Operator)

The NOR logical operator gives a result of true if and only if both operands are false. In other words, it returns false only if at least one of its operands is true.

The NOR logical operator is a truth-functional operator in boolean logic that creates the negation of logical OR. In other words, a statement of the type (a NOR b) is true when neither condition a nor condition b is true, i.e., when both conditions a and b are false.

The table below clearly explains how NOR logical operator works based on the different inputs.

Comparing Boolean values of Two Conditions Result
True True False
True False False
False True False
False False True

While using NOR Logical operation in a programming language, we can relate NOR(ab) as NOT(OR(ab)), which in infix notation is - !(a or b).

According to De Morgan's LawsNOR logical operator also equates to - (!aand (!b).

Let us understand the working of NOR operator in different programming languages with various examples.

Example 1 - Conditional Example of NOR in Python

Below is the Python Code -

# Conditional Examples of NOR Logical operator in Python

condition1 = True
condition2 = True

condition3 = False
condition4 = False

# Method #1
print(not (condition1 or condition2))
print(not (condition1 or condition3))
print(not (condition3 or condition2))
print(not (condition3 or condition4))

print('-'*10)

# Method #2
print((not (condition1)) and (not (condition2)))
print((not (condition1)) and (not (condition3)))
print((not (condition3)) and (not (condition2)))
print((not (condition3)) and (not (condition4)))

The Output will be -

False
False
False
True
----------
False
False
False
True

Example 2 - Conditional Example of NOR in C++

Below is the C++ Code -

#include<iostream>
using namespace std;

int main()
{
    // Conditional Examples of NOR Logical operator in C++

    bool condition1 = true;
    bool condition2 = true;

    bool condition3 = false;
    bool condition4 = false;
    
    // Method #1
    cout << (!(condition1 || condition2)) << ' ';
    cout << (!(condition1 || condition3)) << ' ';
    cout << (!(condition3 || condition2)) << ' ';
    cout << (!(condition3 || condition4)) << ' ';

    cout << endl;

    //  Method #2
    cout << ((!(condition1)) && (!(condition2))) << ' ';
    cout << ((!(condition1)) && (!(condition3))) << ' ';
    cout << ((!(condition3)) && (!(condition2))) << ' ';
    cout << ((!(condition3)) && (!(condition4))) << ' ';

}

The Output will be -

0 0 0 1
0 0 0 1

Example 3 - Conditional Example of NOR in Java

Below is the Java Code -

package test1;

public class rough {

	public static void main(String[] args) {
		
		// Conditional Examples of NOR Logical operator in Java
		
		boolean condition1 = true;
	    boolean condition2 = true;

	    boolean condition3 = false;
	    boolean condition4 = false;
		
	    // Method #1
	    System.out.println(!(condition1 || condition2));
	    System.out.println(!(condition1 || condition3));
	    System.out.println(!(condition3 || condition2));
	    System.out.println(!(condition3 || condition4));

	    System.out.println();

	    //  Method #2
	    System.out.println((!(condition1)) && (!(condition2)));
	    System.out.println((!(condition1)) && (!(condition3)));
	    System.out.println((!(condition3)) && (!(condition2)));
	    System.out.println((!(condition3)) && (!(condition4)));
	}

}

The Output will be -

false
false
false
true

false
false
false
true

Example 4 - Using NOR in Python with list and loop

Below is the Python Code -

# Examples of NOR Logical operator in Python

numbers_all = [8, 11, 14, 16, 17, 20]

# Method #1 - not ((condition1) or (condition2))
for i in numbers_all:
    if not ((i > 15) or (i % 2 != 0)):
        print(i)

print('-'*10)

# Method #2 - (not(condition1)) and (not(condition2))
for i in numbers_all:
    if ((not (i > 15)) and (not (i % 2 != 0))):
        print(i)

The Output will be -

8
14
----------
8
14

Example 5 - Using NOR in C++ with array and loop

Below is the C++ Code -

#include<iostream>
using namespace std;

int main()
{
    // Examples of NOR Logical operator in C++
    int numbers_all[] = {8, 11, 14, 16, 17, 20};
    
    // Method #1 - ! ((condition1) || (condition2))
    for(int i : numbers_all)
    {
        if ( !((i > 15) || (i % 2 != 0)) )
        {
            cout << i <<" ";
        }   
    }

    cout << endl;

    // Method #2 - (!(condition1)) && (!(condition2))
    for(int i : numbers_all)
    {
        if ((! (i > 15)) && (! (i % 2 != 0)))
        {
            cout << i <<" ";
        }   
    }
}

The Output will be -

8 14
8 14

Example 6 - Using NOR in Java with array and loop

Below is the Java Code -

package test1;

public class rough {

	public static void main(String[] args) {
		
		// Examples of NOR Logical operator in Java
		
        int numbers_all[] = {8, 11, 14, 16, 17, 20};


     // Method #1 - ! ((condition1) || (condition2))
    	for(int i : numbers_all)
      	{
      		if ( !((i > 15) || (i % 2 != 0)) )
      		{
      			System.out.print(i);
      			System.out.print(' ');
    		}	
      	}

    	System.out.println();

        // Method #2 - (!(condition1)) && (!(condition2))
        for(int i : numbers_all)
      	{
      		if ((! (i > 15)) && (! (i % 2 != 0)))
      		{
      			System.out.print(i);
      			System.out.print(' ');
    		}	
      	}

	}

}

The Output will be -

8 14
8 14

I hope you found this article helpful.

NOR (Logical Operator) – Explained with Programming Examples - FI

You may also like to explore -

Polynomial class Problem – Java, Python, C++

Pairs with difference of K – in Java, C++ and Python

Cheers!

Happy Coding.

About the Author

This article was authored by Rawnak.