Professional Documents
Culture Documents
6
6
Construction
of
Combinational
Circuits
4-bit binary
Adder-
Subtractor
Difference
between
EPROM and
EEPROM
Difference
between
Decoder and
Demultiplexer
Prime
Implicant chart
for minimizing
Cyclic Boolean
functions
Difference
⇣
between DDR
and SDRAM
Counting
Boolean
function with
some variables
1 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
1. AND gate,
2 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
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:
Product Method
3 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
// 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;
& Operator
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, and_ans;
⇣
If-Else
4 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
// 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;
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.
5 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
+ 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;
| Operator
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
▲
|| Operator
6 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
// 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;
If-Else
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int b[5] = { 0, 1, 1, 0, 0 };
int i, or_ans;
⇣
Output:
▲
1 AND 0 = 1
0 AND 1 = 1
7 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
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 other-
wise.
Below are the programs to implement NAND gate using various methods:
1. Using if else.
2. Using Complement of the product.
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, ans;
8 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
// 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;
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.
⇣
Ad closed by
Stop seeing this ad
Below are the programs to implement NOR gate using various methods:
9 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
1. Using + Operator.
2. Using if else.
+ 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, ans;
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, ans;
Output:
1 NOR 0 = 0 ▲
0 NOR 1 = 0
1 NOR 1 = 0
10 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 0, 1, 0, 1 };
int i, ans;
! Operator
11 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
// 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;
Output:
NOT 1 = 0
NOT 0 = 1
NOT 1 = 0
NOT 0 = 1
NOT 1 = 0
Recommended Posts:
Introduction of Logic Gates
Half Adder and Half Subtractor using NAND NOR gates
⇣
12 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
sunilkannur98
Talk is cheap Show me the code -)
If you like GeeksforGeeks and would like to contribute, you can also write an article using con-
tribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article
appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you �nd anything incorrect by clicking on the "Improve Article" but-
ton below.
2
3
To-do Done Based on 1 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
⇣
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
13 of 14 06/12/19, 3:32 pm
Program to implement Logic Gates - GeeksforGeeks https://www.geeksforgeeks.org/program-to-imp...
14 of 14 06/12/19, 3:32 pm