jaredec18

Untitled

Sep 12th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. Solution for the above question are as follows -
  2.  
  3. Code :
  4.  
  5. import java.util.*;
  6.  
  7. public class Main {
  8. // generateTruthTable function defination
  9. public static void generateTruthTable(int n) {
  10. // get total no of rows
  11. int r = (int) Math.pow(2,n);
  12. // for loop over rows
  13. for (int i=0; i<r; i++) {
  14. for (int j=n-1; j>=0; j--) {
  15. // check the truth table condition
  16. if ((i/(int) Math.pow(2, j))%2 == 0) {
  17. System.out.print("T ");
  18. } else {
  19. System.out.print("F ");
  20. }
  21. }
  22. // print new line
  23. System.out.println();
  24. }
  25. }
  26. public static void main(String[] args) {
  27. // get input from user
  28. Scanner sc = new Scanner(System.in);
  29. System.out.print("Enter Positive Number: ");
  30. int n = sc.nextInt();
  31. // call to generateTruthTable function
  32. generateTruthTable(n);
  33. }
  34. }
  35.  
  36. Code Screen Shot :
  37. https://media.cheggcdn.com/media/d85/d8538825-2b34-4073-a878-a7bfd83539bc/phpszjHVz.png
  38.  
  39. Output :
  40.  
  41. 1)https://media.cheggcdn.com/media/fe3/fe38a4ff-8429-4b63-aa07-f9225345aa0c/phpWP7De0.png
  42.  
  43. 2)https://media.cheggcdn.com/media/73e/73ef339c-1290-46e0-9bf5-9bd2f7835806/phpQixsrc.png
  44.  
  45. 3)https://media.cheggcdn.com/media/95d/95df3bca-d0ec-47a5-abed-83b0937632bd/php6CRkJC.png
Advertisement
Add Comment
Please, Sign In to add comment