Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Solution for the above question are as follows -
- Code :
- import java.util.*;
- public class Main {
- // generateTruthTable function defination
- public static void generateTruthTable(int n) {
- // get total no of rows
- int r = (int) Math.pow(2,n);
- // for loop over rows
- for (int i=0; i<r; i++) {
- for (int j=n-1; j>=0; j--) {
- // check the truth table condition
- if ((i/(int) Math.pow(2, j))%2 == 0) {
- System.out.print("T ");
- } else {
- System.out.print("F ");
- }
- }
- // print new line
- System.out.println();
- }
- }
- public static void main(String[] args) {
- // get input from user
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter Positive Number: ");
- int n = sc.nextInt();
- // call to generateTruthTable function
- generateTruthTable(n);
- }
- }
- Code Screen Shot :
- https://media.cheggcdn.com/media/d85/d8538825-2b34-4073-a878-a7bfd83539bc/phpszjHVz.png
- Output :
- 1)https://media.cheggcdn.com/media/fe3/fe38a4ff-8429-4b63-aa07-f9225345aa0c/phpWP7De0.png
- 2)https://media.cheggcdn.com/media/73e/73ef339c-1290-46e0-9bf5-9bd2f7835806/phpQixsrc.png
- 3)https://media.cheggcdn.com/media/95d/95df3bca-d0ec-47a5-abed-83b0937632bd/php6CRkJC.png
Advertisement
Add Comment
Please, Sign In to add comment