Hi can anyone help me in doing the following

Given n Boolean variables x1,x2,…..xn, we wish to print all possible combinations of truth values they can assume. For instance, if n=2, there are four possibilities : true, true; false, false; false, true; true, false;:-|

## All 3 Replies

Simple. Just use nested for loops.

``````bool x1 = {true, false}
bool x2 = {true, false}
for ( i = 0 ; i < 1; i++ )
for ( j = 0 ; j < 1; j++ )
print x1[i], x2[j];``````

will print the answer for two variables. Expand it for n variables.

Maybe something like this which will implement a basic AND logic will help you understand the thing better:

``````int main ()
{
bool first [] = {true, false} ;
bool second [] = {true, false} ;

for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
cout << boolalpha << first [i] << "  AND  " << second [j] << "  =>  " << (first[i] && second [j]) ;
cout << endl ;
}
}
return 0 ;
}``````

Hope it helped, bye.

`for ( i = 0 ; i < 1<<N ; i++ )` Just print out the binary representation of i to N bits of precision.
Easy money.

commented: nice (andor) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.