Here in this program, we will write a code that will print the truth table for XY+Z.

Truth Table for XY+Z

C++:

#include<iostream.h>
#include< conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
int a,b,c;
cout<<"A\tB\tC\tAB+C";
for(a=0;a<=1;a++)
for(b=0;b<=1;b++)
for(c=0;c<=1;c++)
{
if(a*b+c==2)
cout<<"\n\n"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<"1";
else
cout<<"\n\n"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<a*b+c;
}
getch();
}

Output:

A       B       C       AB+C
0       0       0       0
0       0       1       1
0       1       0       0
0       1       1       1
1       0       0       0
1       0       1       1
1       1       0       1
1       1       1       1

Python:

print("A\tB\tC\tAB+C")
for a in range(0,2):
    for b in range(0,2):
        for c in range(0,2):
            if a*b+c==2:
                print("\n"+str(a)+"\t"+str(b)+"\t"+str(c)+"\t1")
            else:
                print("\n"+str(a)+"\t"+str(b)+"\t"+str(c)+"\t"+str(a*b+c))

Output:

A       B       C       AB+C
0       0       0       0
0       0       1       1
0       1       0       0
0       1       1       1
1       0       0       0
1       0       1       1
1       1       0       1
1       1       1       1

Leave a Reply

Your email address will not be published. Required fields are marked *