Signup/Sign In

C++ Program Print Truth Table Of XY+Z Using Loop

We need to write a program that can print a truth table for the logic XY+Z.

The XY+Z logic shows a AND operator between X and Y, and an OR operator between XY and Z.

C++ Program Print Truth Table Of XY+Z Using Loop

The algorithm for this logic is pretty simple. We just need to create a nested three-level loop where the outermost loop represents the X value, the second loop represents the Y value, and the third last loop represents the Z value. And inside the Z value, we will print and set the logic for the XY+Z table using logical operators.

All the programming languages support the basic logic Operators like AND (&&), OR (||), and NOT (!)

#include<iostream>
using namespace std;

int main()
{
	int X, Y, Z;
	printf("X \t Y \t \Z \t XY+Z\n");
	
	//X value range 0 to 1
	for(X=0; X<=1; X++)
	{	
		//Y value range 0 to1
		for(Y=0;Y<=1; Y++)
		{
			//Z value range 0 to1
			for(Z=0;Z<=1;Z++)
			{
				//check for the XY+Z True values
				if((X &&Y) || Z)
				{
					//print 1 for the true value
					cout<<("%d \t %d \t %d \t 1\n", X,Y, Z );
				}
				else
				{
					//print 0 for the false value
					cout<<("%d \t %d \t %d \t 0\n", X,Y, Z );
				}
			}
		}
	}

    return 0;
}


X Y Z XY+Z
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

Here, in this tutorial we have learned how to write and implement C++ Program Print Truth Table Of XY+Z Using Loop.