"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, February 25, 2010

WAP to print the truth table for XY + Z

Truth Table for XY+Z

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

/* Logic is
if Z is 1 then output is 1 ;
or
if X and Y both are 1 , the output is 1 . */

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
cout<< "Truth Table\n";
cout<< "X\t Y\t Z\t\t XY+Z \n\n";

for(int i = 0 ; i<=1 ; i++)
for(int j = 0; j<=1; j++)
for(int k = 0; k<=1; k++)
{
cout<< i<< "\t"<< j << "\t" << k << "\t\t";

if( (i == 1 && j == 1) || k == 1)
cout<< 0;
else
cout<< 1;

cout<<"\n";
}

getch();

}

3 comments:

  1. There is a mistake.. it shud be

    if( (i == 1 && j == 1) || k == 1)
    cout<< 1;
    else
    cout<< 0;

    ReplyDelete
  2. what about this??


    #include(iostream)
    #include(conio.h)

    using namespace std;

    void main()
    {
    int X,Y,Z;
    int xyPlusZ;
    cout<<"\t\tTRUTH TABLE \n"
    <<"X\tY\tZ\t(XY)+Z\n"
    <<"---\t---\t---\t---\n\n"; // for the table 1st row
    for(X=0;X<=1;X++)
    {
    for(Y=0;Y<=1;Y++)
    {
    for(Z=0;Z<=1;Z++)
    { xyPlusZ=((X&&Y)||Z); // for the value
    cout<<X<<"\t"<<Y<<"\t"<<Z<<"\t"<<xyPlusZ<<endl; // table content

    }
    }
    }
    getch();
    }

    ReplyDelete

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.