5.22 (De Morgan’s Laws) In this chapter, we discussed the logical operators &&, || and !. De Morgan’s laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !(condition1 && condition2) is logically equivalent to the expression (!condition1 || !condition2). Also, the expression ! (condition1 || condition2) is logically equivalent to the expression (!condition1 && !condition2). Use De Morgan’s laws to write equivalent expressions for each of the following, then write an application to show that both the original
expression and the new expression in each case produce the same value:
A. !(x < 5) && !(y >= 7)
B. !(a == b) || !(g != 5)
C. !((x <= 8) && (y > 4))
D. !((i > 4) || (j <= 6))
Code:
#include<iostream>
using namespace std;
int main(){
int a=2;
int b=4;
int x=8;
int y=7;
int i=2;
int j=3;
int g=5;
if(!(x < 5) && !(y >= 7) == !(x < 5) || !(y >= 7) ){
cout<<"!(x < 5) && !(y >= 7) is equivalent to !(x < 5) || !(y >= 7) "<<endl;
}
else{
cout<<"!(x < 5) && !(y >= 7) is not equivalent to !(x < 5) || !(y >= 7) "<<endl;
}
if(!(a == b) || !(g != 5) == !(a == b) && !(g != 5) ){
cout<<" !(a == b) || !(g != 5) is equivalent to !(a == b) && !(g != 5)"<<endl;
}
else{
cout<<" !(a == b) || !(g != 5) is not equivalent to !(a == b) && !(g != 5)"<<endl;
}
if(!((x <= 8) && (y > 4)) == !((x <= 8) || (y > 4)) ){
cout<<" !((x <= 8) && (y > 4)) is equivalent to !((x <= 8) || (y > 4)) "<<endl;
}
else{
cout<<" !((x <= 8) && (y > 4)) is not equivalent to !((x <= 8) || (y > 4)) "<<endl;
}
if((!((i > 4) || (j <= 6) == !((i > 4) && (j <= 6)))))
{
cout<<"!((i > 4) || (j <= 6)) is equivalent to !((i > 4) && (j <= 6))"<<endl;
}
else{
cout<<"!((i > 4) || (j <= 6)) is not equivalent to !((i > 4) && (j <= 6))"<<endl;
}
return 0;
}