Write a Program to print the truth table for XY+Z

Posted in

Write a Program to print the truth table for XY+Z
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    Problem Statement

    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 .

    Algorithm

    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 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 (!).

    C Program to print the truth table for XY+Z

    #include<stdio.h>
    
    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
    					printf("%d \t %d \t %d \t 1\n", X,Y, Z );
    				}
    				else
    				{
    					//print 0 for the false value
    					printf("%d \t %d \t %d \t 0\n", X,Y, Z );
    				}
    			}
    		}
    	}
    
        return 0;
    }

    Output

    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

    C++ Program to print the truth table for XY+Z

    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int X, Y, Z;
    	cout<<"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<<X<< " \t "<<Y<<" \t "<<Z<<" \t 1\n";
    				}
    				else
    				{
    					//print 0 for the false value
    					cout<<X<< " \t "<<Y<<" \t "<<Z<<" \t 0\n";
    				}
    			}
    		}
    	}
    
        return 0;
    }

    Output

    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

    Python Program to print the truth table for XY+Z

    print("X \t Y \t Z \t XY+Z")
    # //X value range 0 to 1
    for X in range(0,2):
        # Y value range 0 to 1
        for Y in range(0,2):
            # Z value range 0 to 1
            for Z in range(0,2):
                # check for the XY+Z True values
                if (X and Y) or Z:
                    # print 1 for the true value
                    print(f"{X} \t {Y} \t {Z} \t 1")
                else:
                    # print 0 for the false value
                    print(f"{X} \t {Y} \t {Z} \t 0")

    Output

    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

    Wrapping Up!

    To build a truth table we just need the N numbers of nested for loops, where N represents the number of variables used in the truth table. In the above program we only have 3 variables X, Y, and Z for that we only required 3 nested for loops. The logic for the program can be written inside the last loop using the conditional statement and logical operators. Using the same pattern as the above program we can write any truth table for 3 variables such as XYZ, X+Y+Z, X+YZ, etc.

    People are also reading:

    Leave a Comment on this Post

    0 Comments