For each of the following, write the requested program or subroutine in pseudocode (as described in Appendix A) or in a programming language that you know. Test your code either with a paper-and-pencil trace or with a computer run.
1. Write a program that will print a truth table for (p ∨ q) ⇒ r. (Use JAVA language)
public class Solution
{
public static void main(String[] args) {
System.out.println("p\tq\tr");
int p,q,r;
for(int i=0;i<4;i++) //as we have only 4
choices
{
r = i; //r is here as a temporary variable
p = i%2;
r /= 2;
q = r%2;
System.out.println(p + "\t" + q + "\t" + (p|q));
}
}
}
We take P as i%2 because in first iteration P will be 0 then 1 then again 0 then again 1
in the similiar way when i is 0 Q will be 0
then when i is 1 Q will be 0
then when i is 2 Q will be 1 as 1%2 == 0
then when i is 3 Q will be 1
I hope I am able to clear the logic of the problem.
Get Answers For Free
Most questions answered within 1 hours.