Problem Statement
The De Morgan’s law state that (A+B)’=A’B’ and (AB)’=A’+B’. Use De Morgan’s laws to write equivalent expression for each of the following, then write application to show that both original and new expression in each case produce the same value.
- a) !(x<5) && !(y>=7)
- b) !((I>4) || (j <=6))
Algorithm:
Begin
Input values for x and y.
Apply De Morgan’s law (A+B)|=A|B| and (AB)|=A|+B| and check for the equality of the given expressions.Display the messages accordingly.
End
Program:
import java.*; import java.util.Scanner; class Demorgan { int x,y,i,j; void readNumbers() { Scanner Sc = new Scanner(System.in); System.out.println("x : "); x = Sc.nextInt(); System.out.println("y : "); y = Sc.nextInt(); System.out.println("i : "); i = Sc.nextInt(); System.out.println("j : "); j = Sc.nextInt(); } void FirstLaw() { boolean result; result = !(!(x<5) && !(y>=7)); System.out.println("!(AB) = "+result); result = ((x<5) || (y>=7)); System.out.println("!A + !B = "+result); } void SecondLaw() { boolean result; result = !((i>4) || (j <=6)); System.out.println("!(A+B) = "+result); result = (!(i>4) && !(j <=6)); System.out.println("!A!B = "+result); } } public class Demor { public static void main(String[] args) { Demorgan obj = new Demorgan(); obj.readNumbers(); obj.FirstLaw(); obj.SecondLaw(); } } |
Output:
x : 5
y : 6
i : 7
j : 9
!(AB) = false
!A + !B = false
!(A+B) = false
!A!B = false
x : 4
y : 6
i : 10
j : 6
!(AB) = true
!A + !B = true
!(A+B) = false
!A!B = false
x : 7
y : 2
i : 1
j : 8
!(AB) = false
!A + !B = false
!(A+B) = true
!A!B = true
Eve says
Great hammer of Thor, that is pollfruwey helpful!