This tutorial will help you learn how to write a program in C, C++, Python, and Java to print the truth table for XY+Z.
To implement the truth table for XY+Z, you must know the programming language syntax, the for loop, and the logic to write the truth table for XY+Z.
Before writing a program, let us understand what the truth table is.
What is a Truth Table?
A truth table is a tabular representation of all possible combinations of values for inputs and outputs. In other words, it is a tabular representation of all possible outcomes from all possible factual scenarios. Hence, we name the table as a truth table. It is primarily used with Boolean algebra, Boolean functions, and prepositional calculus.
Truth Table for XY+Z
We have the expression XY + Z, where X, Y, and Z can take either True (1) or False (0) values. The operator '+' represents the OR operation, and XY uses the AND operation.
So, the truth table for XY+Z becomes:
X | Y | Z | XY | XY + Z |
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 1 |
0 | 1 | 0 | 0 | 0 |
0 | 1 | 1 | 0 | 1 |
1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 1 |
1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 1 |
C Program to Print the Truth Table for XY+Z
#include<stdio.h>
int main()
{
int a,b,c;
printf("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)
printf("\n\n %d \t %d \t %d \t 1", a, b, c);
else
printf("\n\n %d \t %d \t %d \t %d", a,b,c, (a*b+c));
}
return 0;
}
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
C++ Program to Print the Truth Table for XY+Z
#include<iostream>
using namespace std;
int main()
{
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;
}
return 0;
}
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 Program to Print the Truth Table for XY+Z
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
Java Program to Print the Truth Table for XY+Z
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a,b,c;
System.out.println("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)
System.out.println("\n"+a+ "\t" +b+ "\t" +c+ "\t1");
else
System.out.println("\n"+a+ "\t" +b+ "\t"+c+ "\t" +(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
Conclusion
Wasn't it interesting to print the truth table for XY+Z? Now, you can print the truth table for any given expression. The only thing you need to take care of in such types of programs is using the for loop and spacing. We used the new line character ('\n') and horizontal tab character ('\t').
If you encounter any issue, do let us know in the comments.
People are also reading:
- WAP in C++ and python to check whether the number is even or odd
- Python Program to Add Two Matrices
- WAP to check whether a given character is an alphabet, digit or any special character
- Python Program to Find Sum of Natural Numbers Using Recursion
- Write a C++ Calculator to perform Arithmetic Operations using Switch Case
- Python Program to Multiply Two Matrices
- WAP in C to check whether the number is a Palindrome number or not
- Python Program to Shuffle a Deck of Cards
- WAP in C++ & Python for Armstrong Number
- Python Program to Swap Two Variables
Leave a Comment on this Post