C program to print the truth table for XY+Z
Here is the C program to print the truth table for XY+Z:
#include
int main() {
int x, y, z, f;
printf("Truth Table\n");
printf("x y z f\n");
for (x = 0; x <= 1; x++) {
for (y = 0; y <= 1; y++) {
for (z = 0; z <= 1; z++) {
f = x & y | z;
printf("%d %d %d %d\n", x, y, z, f);
}
}
}
return 0;
}
Output

Explanation:
- The program starts by including the necessary header file
stdio.h
, which allows us to use input/output functions such asprintf
. - We declare three integer variables
X
,Y
, andZ
to represent the values of the variables in the logical expression XY+Z. - We use
printf
statements to display a header for the truth table, including column labels for X, Y, Z, and the output. - The program uses three nested
for
loops to iterate through all possible combinations of values for X, Y, and Z. Each loop runs from 0 to 1, representing the two possible binary values (0 or 1). - Inside the innermost loop, we calculate the output of the logical expression XY+Z by multiplying X and Y and then adding Z. We store the result in the
output
variable. - Finally, we use another
printf
statement to display the values of X, Y, Z, and the output in a tabular format.
When you run the program, it will generate the truth table for the logical expression XY+Z, showing all possible combinations of values for X, Y, and Z, along with the corresponding output.