FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Boolean Truth Table for AND, OR and NOT using C, C++ , Java and Python




❮ Previous    Next ❯


A Boolean Truth Table is used to represent all possible input combinations and the corresponding output of a Boolean operation. In this experiment, we shall create a program to display the Truth Tables for the three fundamental Boolean operations:

  • AND
  • OR
  • NOT

The programs will be implemented using C, C++, Java and Python.


Objective

To write a program in C, C++, Java and Python to display the Boolean Truth Tables for AND, OR and NOT operations.


Theory

Boolean Algebra deals with values that have only two possible states:

0 = False

1 = True

Boolean operations are widely used in computer science, digital electronics, programming, database systems and computer architecture. The three fundamental Boolean operations are AND, OR and NOT.


1. AND Operation

The AND operation produces output 1 only when both inputs are 1. The Boolean expression is:

Y = A AND B

Y = A · B

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

Remember: AND gives 1 only when all inputs are 1.


2. OR Operation

The OR operation produces output 1 when at least one input is 1. The Boolean expression is:

Y = A OR B

Y = A + B

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

Remember: OR gives 0 only when all inputs are 0.


3. NOT Operation

The NOT operation works on only one input. It reverses or complements the input value. The Boolean expression is:

Y = NOT A

Y = A̅

A NOT A
0 1
1 0

Remember: NOT simply reverses the Boolean value.


Combined Truth Table

A B A AND B A OR B NOT A
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

Number of Rows in a Truth Table

For n Boolean input variables, the number of possible input combinations is:

2ⁿ

For two inputs A and B:

2² = 4

Therefore, the AND and OR truth tables contain 4 rows. For one input, as in the NOT operation:

2¹ = 2

Therefore, the NOT truth table contains 2 rows.


Algorithm

  1. Start.
  2. Display the headings for AND, OR and NOT operations.
  3. Generate all possible combinations of Boolean values A and B.
  4. Perform the AND operation using the logical AND operator.
  5. Perform the OR operation using the logical OR operator.
  6. Perform the NOT operation on A.
  7. Display the results in tabular form.
  8. Stop.

Flowchart

Boolean Truth Table for AND OR and NOT Flowchart


C Program

The following C program displays the Truth Tables for AND, OR and NOT using logical operators.


/* C Program to Display Boolean Truth Tables */

#include <stdio.h>

int main()
{
    int A, B;

    printf("Boolean Truth Table\n\n");

    printf(" A  B  AND  OR  NOT A\n");

    printf("---------------------\n");

    for(A = 0; A <= 1; A++)
    {
        for(B = 0; B <= 1; B++)
        {
            printf(" %d  %d   %d    %d     %d\n",
                   A,
                   B,
                   A && B,
                   A || B,
                   !A);
        }
    }

    return 0;
}

Sample Output (C)

Boolean Truth Table

 A  B  AND  OR  NOT A
---------------------
 0  0   0    0     1
 0  1   0    1     1
 1  0   0    1     0
 1  1   1    1     0

Explanation of the C Program

  • A and B represent Boolean input variables.
  • The nested loops generate all four possible combinations.
  • && performs the logical AND operation.
  • || performs the logical OR operation.
  • ! performs the logical NOT operation.
  • The results are displayed in tabular form.

Important C Operators

Operator Operation Example
&& Logical AND A && B
|| Logical OR A || B
! Logical NOT !A

C++ Program


/* C++ Program to Display Boolean Truth Tables */

#include <iostream>

using namespace std;

int main()
{
    int A, B;

    cout << "Boolean Truth Table\n\n";

    cout << " A  B  AND  OR  NOT A\n";

    cout << "---------------------\n";

    for(A = 0; A <= 1; A++)
    {
        for(B = 0; B <= 1; B++)
        {
            cout << " "
                 << A << "  "
                 << B << "   "
                 << (A && B) << "    "
                 << (A || B) << "     "
                 << (!A) << endl;
        }
    }

    return 0;
}

Sample Output (C++)

Boolean Truth Table

 A  B  AND  OR  NOT A
---------------------
 0  0   0    0     1
 0  1   0    1     1
 1  0   0    1     0
 1  1   1    1     0

C++ Operators Used

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Java Program

The following Java program displays the Truth Tables for AND, OR and NOT operations.


/* Java Program to Display Boolean Truth Tables */

public class BooleanTruthTable
{
    public static void main(String[] args)
    {
        System.out.println("Boolean Truth Table\n");

        System.out.println(" A  B  AND  OR  NOT A");

        System.out.println("---------------------");

        for(int A = 0; A <= 1; A++)
        {
            for(int B = 0; B <= 1; B++)
            {
                int andResult = (A == 1 && B == 1) ? 1 : 0;

                int orResult = (A == 1 || B == 1) ? 1 : 0;

                int notResult = (A == 0) ? 1 : 0;

                System.out.println(
                    " " + A +
                    "  " + B +
                    "   " + andResult +
                    "    " + orResult +
                    "     " + notResult
                );
            }
        }
    }
}

Sample Output (Java)

Boolean Truth Table

 A  B  AND  OR  NOT A
---------------------
 0  0   0    0     1
 0  1   0    1     1
 1  0   0    1     0
 1  1   1    1     0

Python Program

Python provides the logical operators and, or and not for Boolean operations.


# Python Program to Display Boolean Truth Tables

print("Boolean Truth Table\n")

print(" A  B  AND  OR  NOT A")

print("---------------------")

for A in [0, 1]:

    for B in [0, 1]:

        AND = int(bool(A) and bool(B))

        OR = int(bool(A) or bool(B))

        NOT_A = int(not bool(A))

        print(A, " ", B, "   ",
              AND, "    ",
              OR, "     ",
              NOT_A)

Sample Output (Python)

Boolean Truth Table

 A  B  AND  OR  NOT A
---------------------
0   0    0      0       1
0   1    0      1       1
1   0    0      1       0
1   1    1      1       0

Python Using Boolean Values

Python can also work directly with True and False.


# Boolean Truth Table using True and False

values = [False, True]

print(" A       B       AND       OR       NOT A")

for A in values:

    for B in values:

        print(A, "   ", B,
              "   ", A and B,
              "   ", A or B,
              "   ", not A)

Boolean Operators in Python

Operator Operation Example
and Logical AND A and B
or Logical OR A or B
not Logical NOT not A

Truth Table using Python Functions


# Boolean Truth Table using Functions

def AND(A, B):

    return A and B


def OR(A, B):

    return A or B


def NOT(A):

    return not A


print("A B AND OR NOT A")

for A in [False, True]:

    for B in [False, True]:

        print(
            int(A),
            int(B),
            int(AND(A, B)),
            int(OR(A, B)),
            int(NOT(A))
        )

Dry Run

For two Boolean variables A and B, there are:

2² = 4

possible combinations.

Step A B A AND B A OR B NOT A
1 0 0 0 0 1
2 0 1 0 1 1
3 1 0 0 1 0
4 1 1 1 1 0

Step-by-Step Dry Run

Case 1: A = 0, B = 0

AND: 0 AND 0 = 0

OR: 0 OR 0 = 0

NOT: NOT 0 = 1


Case 2: A = 0, B = 1

AND: 0 AND 1 = 0

OR: 0 OR 1 = 1

NOT: NOT 0 = 1


Case 3: A = 1, B = 0

AND: 1 AND 0 = 0

OR: 1 OR 0 = 1

NOT: NOT 1 = 0


Case 4: A = 1, B = 1

AND: 1 AND 1 = 1

OR: 1 OR 1 = 1

NOT: NOT 1 = 0


Time Complexity

For two Boolean variables, there are only four possible combinations. The program examines each combination once. Therefore, the time complexity is:

O(1)

For a general truth table containing n Boolean variables, there are 2ⁿ possible combinations. If each combination is processed in constant time, the complexity is:

O(2ⁿ)


Space Complexity

The program uses only a small number of variables to calculate the Boolean results. Therefore, the auxiliary space complexity is:

O(1)


Comparison of AND, OR and NOT

Operation Number of Inputs Output Rule Operator
AND 2 1 only when both inputs are 1 &&
OR 2 1 when at least one input is 1 ||
NOT 1 Reverses the input !

Applications

  • Digital Electronics
  • Logic Gates
  • Computer Architecture
  • Database Queries
  • Conditional Statements
  • Search Engines
  • Artificial Intelligence
  • Boolean Algebra
  • Programming Languages
  • Decision Making Systems

Advantages

  • Simple way to understand Boolean operations.
  • Helps verify logical expressions.
  • Useful for designing digital circuits.
  • Useful in programming and database queries.
  • Provides all possible input-output combinations.

Disadvantages

  • The number of rows grows exponentially with the number of variables.
  • Large Boolean expressions can produce very large truth tables.
  • Manual construction becomes difficult for many variables.

Viva Questions

  1. What is Boolean Algebra?
  2. What are the two Boolean values?
  3. What is a Truth Table?
  4. What is the AND operation?
  5. What is the OR operation?
  6. What is the NOT operation?
  7. Which operator is used for logical AND in C?
  8. Which operator is used for logical OR in C?
  9. Which operator is used for logical NOT in C?
  10. How many rows are present in a truth table with two variables?
  11. What is the formula for the number of rows in a truth table?
  12. What is the time complexity for n Boolean variables?

Frequently Asked Interview Questions

  1. What is a Truth Table?
    A Truth Table is a table that displays all possible combinations of Boolean inputs and their corresponding outputs.

  2. When does AND return 1?
    AND returns 1 only when all its inputs are 1.

  3. When does OR return 1?
    OR returns 1 when at least one input is 1.

  4. What does NOT do?
    NOT complements the input. It changes 0 to 1 and 1 to 0.

  5. How many rows are required for n Boolean variables?
    A truth table requires 2ⁿ rows.

  6. What is the difference between logical AND and bitwise AND?
    Logical AND is generally used to combine Boolean conditions, whereas bitwise AND operates on individual bits of integer values.

  7. Why are Truth Tables important in computer science?
    They are used to analyze Boolean expressions, logic circuits, conditional statements, database queries and digital systems.

Practice Questions

  1. Write a C program to display the Truth Table for AND.
  2. Write a C++ program to display the Truth Table for OR.
  3. Write a Java program to display the Truth Table for NOT.
  4. Write a Python program to display Truth Tables for AND, OR and NOT.
  5. Generate a Truth Table for three Boolean variables.
  6. Find the output of A AND B for all possible values of A and B.
  7. Find the output of A OR B for all possible values of A and B.
  8. Find the complement of each possible Boolean value.
  9. Explain the difference between AND, OR and NOT.
  10. Determine the number of rows required for a Truth Table containing five Boolean variables.

Key Takeaways

  • Boolean values are represented by 0 and 1.
  • A Truth Table shows all possible input-output combinations.
  • AND produces 1 only when both inputs are 1.
  • OR produces 1 when at least one input is 1.
  • NOT reverses the Boolean value.
  • For n Boolean variables, there are 2ⁿ possible combinations.
  • C uses &&, || and ! for logical AND, OR and NOT.
  • Python uses and, or and not for Boolean operations.
  • Truth Tables are important in digital logic and programming.

Summary

A Boolean Truth Table represents all possible combinations of Boolean inputs and their corresponding outputs. In this experiment, the three fundamental Boolean operations AND, OR and NOT have been explained and implemented using C, C++, Java and Python. For two Boolean variables, there are 2² = 4 possible input combinations. For n Boolean variables, there are 2ⁿ possible combinations. Truth Tables are widely used in Boolean Algebra, digital electronics, computer architecture, programming and database systems.


AKTU Examination Tip

Students should be able to:

  • Define Boolean Algebra.
  • Define a Truth Table.
  • Explain AND, OR and NOT operations.
  • Draw the Truth Tables.
  • Explain the 2ⁿ rule.
  • Write the algorithm.
  • Draw the flowchart.
  • Write the C Program.
  • Write the C++ Program.
  • Write the Java Program.
  • Write the Python Program.
  • Perform a dry run.
  • Explain the time and space complexity.
  • Explain practical applications of Boolean operations.






❮ Previous    Next ❯