The Wayback Machine - https://web.archive.org/web/20240618041951/https://www.geeksforgeeks.org/program-to-implement-logic-gates/
Open In App

Program to implement Logic Gates

Last Updated : 31 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

A Logic gate is an elementary building block of any digital circuits. It takes one or two inputs and produces output based on those inputs. Outputs may be high (1) or low (0). Logic gates are implemented using diodes or transistors. It can also be constructed using vacuum tubes, electromagnetic elements like optics, molecule etc. In a computer, most of the electronic circuits are made up logic gates. Logic gates are used to create a circuit that performs calculations, data storage or shows off object-oriented programming especially the power of inheritance.

There are seven basic logic gates defined, these are:

  1. AND gate,
  2. OR gate,
  3. NOT gate,
  4. NAND gate,
  5. NOR gate,
  6. XOR gate and
  7. XNOR gate.

Below are the brief details about them along with their implementation:

  1. AND Gate
    The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise.

    Below are the programs to implement AND gate using various methods:

    1. Using product method.
    2. Using if else condition.
    3. Using “and (&)” operator.

    Product Method




    // C program implementing the AND gate
    // through product method.
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, product;
      
        for (i = 0; i < 5; i++) {
      
            // using product method
            product = a[i] * b[i];
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], product);
        }
    }

    
    

    & Operator




    // C program implementing the AND gate
    // using & operator
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, and_ans;
      
        for (i = 0; i < 5; i++) {
      
            // using the & operator
            and_ans = a[i] & b[i];
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], and_ans);
        }
    }

    
    

    If-Else




    // C program implementing the AND gate
    // using if and else condition
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            if (a[i] == 0 && b[i] == 0)
                ans = 0;
      
            else if (a[i] == 0 && b[i] == 1)
                ans = 0;
      
            else if (a[i] == 1 && b[i] == 0)
                ans = 0;
            else
                ans = 1;
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], ans);
        }
    }

    
    

    Output:

    1 AND 0 = 0
     0 AND 1 = 0
     1 AND 1 = 1
     0 AND 0 = 0
     1 AND 0 = 0
    
  2. OR Gate
    The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.

    Below are the programs to implement AND gate using various methods:

    1. Using + operator.
    2. Using | operator.
    3. Using || operator.
    4. Using if else.

    + Operator




    // C program implementing the OR gate
    // using + operator
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, or_ans;
      
        for (i = 0; i < 5; i++) {
      
            // using the + operator
            if (a[i] + b[i] > 0)
                or_ans = 1;
            else
                or_ans = 0;
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], or_ans);
        }
    }

    
    

    | Operator




    // C program implementing the OR gate
    // using | operator
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, or_ans;
      
        for (i = 0; i < 5; i++) {
      
            // using the | operator
            or_ans = a[i] | b[i];
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], or_ans);
        }
    }

    
    

    || Operator




    // C program implementing the OR gate
    // using || operator
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, or_ans;
      
        for (i = 0; i < 5; i++) {
      
            // using the || operator
            or_ans = a[i] || b[i];
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], or_ans);
        }
    }

    
    

    If-Else




    // C program implementing the OR gate
    // using if else
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, or_ans;
      
        for (i = 0; i < 5; i++) {
      
            // using the if-else conditions
            if (a[i] == 0 && b[i] == 0)
                or_ans = 0;
            else
                or_ans = 1;
      
            printf("\n %d AND %d = %d",
                   a[i], b[i], or_ans);
        }
    }

    
    

    Output:

    1 AND 0 = 1
     0 AND 1 = 1
     1 AND 1 = 1
     0 AND 0 = 0
     1 AND 0 = 1
    
  3. NAND Gate
    The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise.

    Below are the programs to implement NAND gate using various methods:

    1. Using if else.
    2. Using Complement of the product.

    If-Else




    // C program implementing the NAND gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            if (a[i] == 1 && b[i] == 1)
                ans = 0;
            else
                ans = 1;
            printf("\n %d NAND %d = %d",
                   a[i], b[i], ans);
        }
    }

    
    

    Complement of the product




    // C program implementing the NAND gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            ans = !(a[i] * b[i]);
            printf("\n %d NAND %d = %d",
                   a[i], b[i], ans);
        }
    }

    
    

    Output:

    1 NAND 0 = 1
     0 NAND 1 = 1
     1 NAND 1 = 0
     0 NAND 0 = 1
     1 NAND 0 = 1
    
  4. NOR Gate
    The NOR gate (negated OR) gives an output of 1 if both inputs are 0, it gives 1 otherwise.

    Below are the programs to implement NOR gate using various methods:

    1. Using + Operator.
    2. Using if else.

    + Operator




    // C program implementing the NOR gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            ans = !(a[i] + b[i]);
            printf("\n %d NOR %d = %d",
                   a[i], b[i], ans);
        }
    }

    
    

    If-Else




    // C program implementing the NOR gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int b[5] = { 0, 1, 1, 0, 0 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            if (a[i] == 0 && b[i] == 0)
                ans = 1;
            else
                ans = 0;
            printf("\n %d NOR %d = %d",
                   a[i], b[i], ans);
        }
    }

    
    

    Output:

    1 NOR 0 = 0
     0 NOR 1 = 0
     1 NOR 1 = 0
     0 NOR 0 = 1
     1 NOR 0 = 0
    
  5. NOT Gate

    It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the result as 0 and vice-versa.

    Below are the programs to implement NOT gate using various methods:

    1. Using ! Operator.
    2. Using if else.

    If-Else




    // C program implementing the NOT gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            if (a[i] == 0)
                ans = 1;
            else
                ans = 0;
            printf("\n  NOT %d = %d", a[i], ans);
        }
    }

    
    

    ! Operator




    // C program implementing the NOT gate
      
    #include <stdio.h>
    #include <stdlib.h>
      
    int main()
    {
      
        int a[5] = { 1, 0, 1, 0, 1 };
        int i, ans;
      
        for (i = 0; i < 5; i++) {
            ans = !(a[i]);
            printf("\n  NOT %d = %d", a[i], ans);
        }
    }

    
    

    Output:

    NOT 1 = 0
      NOT 0 = 1
      NOT 1 = 0
      NOT 0 = 1
      NOT 1 = 0
    


Similar Reads

VHDL code for AND and OR Logic Gates
Prerequisite - Introduction of Logic Gates Design and implement the AND and OR logic gates using VHDL (VHSIC Hardware Description Language) programming language. Different Types of VHDL Modelling Styles The architecture of VHDL code is written in three different coding styles : Dataflow Modelling Behavioral ModellingStructural Modelling 1. Logic De
2 min read
Basic Conversion of Logic Gates
In the digital system, logic gates are the basic building blocks. In these logic gates, we can find the gates having more than one input, but will have only one output. The connection between the input and the output of a gate is based on some logic. Based on this logic, different gates are developed like OR gate, AND gate, NOT gate, and more. The
2 min read
Realization of Logic Gate Using Universal gates
In Boolean Algebra, the NAND and NOR gates are called universal gates because any digital circuit can be implemented by using any one of these two i.e. any logic gate can be created using NAND or NOR gates only. Every logic gate has a representation symbol. The below image shows a graphical representation of all logic gates. 1. Implementation of AN
1 min read
Two Level Implementation of Logic Gates
The term "two-level logic" refers to a logic design that uses no more than two logic gates between input and output. This does not mean that the entire design will only have two logic gates, but it does mean that the single path from input to output will only have two logic gates. In two-level logic, irrespective of the total number of logic gates,
6 min read
Binary Adder with Logic Gates
Binary Adder and logic gates are important elements in digital logic. Binary adders are circuit which adds two binary numbers and results in the sum of the two binary numbers. Logic gates are the circuit in which a signal can pass through if a logic is applied through it. In this article we will discuss about the binary adder with logic gates, bina
8 min read
Introduction of Logic Gates
In Boolean Algebra, there are three basic operations, [Tex]+,\:.\:,\:^\prime       [/Tex]which are analogous to disjunction, conjunction, and negation in propositional logic. Each of these operations has a corresponding logic gate. Apart from these, there are a few other logic gates as well. It was invented by George Boole. Table of Content What is
6 min read
Universal Logic Gates
A logic gate is an active electronic component that uses one or more inputs to produce an output based on the bolean algebra each gate is designed to perform. Though there are various types of gates including AND, OR, NOT, NAND, NOR etc.. NAND and NOR gates are a little special. NAND gate and NOR gate can be termed as universal logic gates since an
7 min read
Difference between Programmable Logic Array and Programming Array Logic
Programmable Logic Array (PLA) and Programming Array Logic (PAL) are the categories of programming logic devices. In PLA or Programmable Logic Array, there are massive functions can be implemented. Whereas in PAL or Programmable Array Logic, there is finite functions can be implemented. The distinction between PLA and PAL is that, PAL have programm
2 min read
Half Adder and Half Subtractor using NAND NOR gates
Introduction:Advantages of using NAND and NOR gates to implement Half Adder and Half Subtractor:Universality: NAND and NOR gates are considered universal gates because they can be used to implement any logical function, including binary arithmetic functions such as addition and subtraction.Cost-effectiveness: NAND and NOR gates are relatively simpl
2 min read
Implementation of Full Adder using NOR Gates
In Digital Logic Circuit, Full Adder is a Digital Logic Circuit that can add three inputs and give two outputs. The three inputs A, B, and input carry as Cin. Cout is represented as output carry and Sum is represented as output. We can say Cout is called the majority 1’s detector, when more than one input is high (logic 1) then its output will be h
8 min read