Program to implement Logic Gates - GeeksforGeeks

31 Oct, 2019
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:
AND gate,OR gate,NOT gate,NAND gate,NOR gate,XOR gate andXNOR gate.
AND gate,
OR gate,
NOT gate,
NAND gate,
NOR gate,
XOR gate and
XNOR gate.
Below are the brief details about them along with their implementation:
AND GateThe 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:Using product method.Using if else condition.Using “and (&)” operator.Product Method& OperatorIf-ElseProduct 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
OR GateThe 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:Using + operator.Using | operator.Using || operator.Using if else.+ Operator| Operator|| OperatorIf-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
NAND GateThe 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:Using if else.Using Complement of the product.If-ElseComplement of the productIf-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
NOR GateThe 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:Using + Operator.Using if else.+ OperatorIf-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
NOT GateIt 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:Using ! Operator.Using if else.If-Else! OperatorIf-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

AND GateThe 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:Using product method.Using if else condition.Using “and (&)” operator.Product Method& OperatorIf-ElseProduct 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

Below are the programs to implement AND gate using various methods:
Using product method.Using if else condition.Using “and (&)” operator.
Using product method.
Using if else condition.
Using “and (&)” operator.
Product Method
& Operator
If-Else
// 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);    }}
// 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);    }}
// 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);    }}
1 AND 0 = 0
 0 AND 1 = 0
 1 AND 1 = 1
 0 AND 0 = 0
 1 AND 0 = 0

OR GateThe 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:Using + operator.Using | operator.Using || operator.Using if else.+ Operator| Operator|| OperatorIf-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

Below are the programs to implement AND gate using various methods:
Using + operator.Using | operator.Using || operator.Using if else.
Using + operator.
Using | operator.
Using || operator.
Using if else.
+ Operator
| Operator
|| Operator
If-Else
// 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);    }}
// 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);    }}
// 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);    }}
// 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);    }}
1 AND 0 = 1
 0 AND 1 = 1
 1 AND 1 = 1
 0 AND 0 = 0
 1 AND 0 = 1

NAND GateThe 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:Using if else.Using Complement of the product.If-ElseComplement of the productIf-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

Below are the programs to implement NAND gate using various methods:
Using if else.Using Complement of the product.
Using if else.
Using Complement of the product.
If-Else
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++) {        if (a[i] == 1 && b[i] == 1)            ans = 0;        else            ans = 1;        printf("\n %d NAND %d = %d",               a[i], b[i], ans);    }}
// 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);    }}
1 NAND 0 = 1
 0 NAND 1 = 1
 1 NAND 1 = 0
 0 NAND 0 = 1
 1 NAND 0 = 1

NOR GateThe 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:Using + Operator.Using if else.+ OperatorIf-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

Below are the programs to implement NOR gate using various methods:
Using + Operator.Using if else.
Using + Operator.
Using if else.
+ Operator
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++) {        ans = !(a[i] + b[i]);        printf("\n %d NOR %d = %d",               a[i], b[i], ans);    }}
// 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);    }}
1 NOR 0 = 0
 0 NOR 1 = 0
 1 NOR 1 = 0
 0 NOR 0 = 1
 1 NOR 0 = 0

NOT GateIt 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:Using ! Operator.Using if else.If-Else! OperatorIf-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

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:
Using ! Operator.Using if else.
Using ! Operator.
Using if else.
If-Else
! 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++) {        if (a[i] == 0)            ans = 1;        else            ans = 0;        printf("\n  NOT %d = %d", a[i], ans);    }}
// 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);    }}
NOT 1 = 0
  NOT 0 = 1
  NOT 1 = 0
  NOT 0 = 1
  NOT 1 = 0

Digital Electronics & Logic Design
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Shift Registers in Digital Logic
IEEE Standard 754 Floating Point Numbers
4-bit binary Adder-Subtractor
Multiplexers in Digital Logic
Magnitude Comparator in Digital Logic
Arrow operator -> in C/C++ with Examples
Modulo Operator (%) in C/C++ with Examples
Structures in C++
Differences between Procedural and Object Oriented Programming
Top 10 Programming Languages to Learn in 2022