-2

given

x=2 rows= pow(2,2)=4 and colums=2

output:

T T
T F
F T
F F

x=3 rows= pow(2,3)=8 and colums=8

output:

T T T
T T F
T F T
T F F
F T T
F T F
F F T
F F F

and so on. it could be to print like this or to create an char array with 2^x rows and x columns and with T/F as values.

I can think of it as for say column number j - change should occur after pow(2,x-(j+1))

thanks; pseudo code or java is preferred

BackSlash
  • 21,927
  • 22
  • 96
  • 136
Nikhil
  • 300
  • 1
  • 5
  • 18

3 Answers3

2

Recursively: take the table for N-1 and prepend T to all lines, then repeat with F, and you get the table for N.

void Print(String Line, int N) {
  if (N== 0) println(Line); else { Print("T " + Line, N-1); Print("F " + Line, N-1); }
}

...
Print("", 5);
....
1

I think it may helps you :

public static void main(String args[]) {
        int size = 2;      // change the size ans get your truth table.
        generateTable(0, size, new int[size]);
    }

    private static void generateTable(int index, int size, int[] current) {
        if (index == size) {
            for (int i = 0; i < size; i++) {
                System.out.print(current[i] + " ");
            }
            System.out.println();
        } else {
            for (int i = 0; i < 2; i++) {
                current[index] = i;
                generateTable(index + 1, size, current);
            }
        }
    }

Output(when pass the size = 2) :

0 0 
0 1 
1 0 
1 1 
Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29
1

How about:

for(double x=Math.pow(2,col-1); x<Math.pow(2,col); x++)
    System.out.println(Integer.toBinaryString((int)x));
}

Just set col to how many columns you want outputted

Revive
  • 2,248
  • 1
  • 16
  • 23