Print Truth Table of XY+Z Using Loop

Beginner

Introduction

In this lab, we will learn how to write a C++ program to print a truth table for the logic XY+Z using loops. The program will show the values of X, Y, Z and the result of XY+Z using logical operators.

Create a new C++ file

Navigate to the ~/project directory and create a new C++ file named main.cpp.

cd ~/project
touch main.cpp

Include Libraries

In this step, we will include the necessary libraries to our program. The iostream library provides basic input and output services for C++ programs.

#include <iostream>
using namespace std;

Declare Variables

In this step, we will declare the necessary integer variables to hold the values of X, Y, and Z.

int X, Y, Z;

Print Truth Table Headers

In this step, we will print the headers for the truth table. We will print X, Y, Z, and XY+Z using a tab space.

printf("X \t Y \t \Z \t XY+Z\n");

Create Nested Loop

In this step, we will create a nested three-level loop to iterate through all the possible combinations of X, Y, and Z. The outermost loop represents the X value, the second loop represents the Y value, and the innermost loop represents the Z value.

//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);
            }
        }
    }
}

Compile and Run the Program

We can compile the main.cpp file using the command g++ main.cpp -o main && ./main in the terminal. After that, we will get the output of the truth table as shown below.

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

Full Code

Below is the full code of the main.cpp file.

#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;
}

Summary

In this lab, we have learned how to write a C++ program to print a truth table of the logic XY+Z using loops. We used a nested three-level loop to iterate through all the possible combinations of X, Y, and Z and used logical operators to compute the result. By completing this lab, we have better understood the basic programming concepts in C++ programming language.

Other Tutorials you may like