Aditya Aditya - 2 months ago 13
Java Question

Write a JAVA program to print below sequence:

Input:

{a, b, c, d}
(Character Array)

I want to generate below sequence using above character array as Input

Output:

a bcd
ab cd
abc d
abcd
a b c d
ab c d
a bc d
a b cd

Answer

You can try,

char[] a = { 'a', 'b', 'c', 'd' };
        for (int i = 0; i < (a.length*2); i++) {
            if (i < a.length) {
                for (int j = 0; j < a.length; j++) {
                    System.out.print(a[j]);
                    if (j == i)
                        System.out.print(" ");
                }
                System.out.println("");
            } else {
                for (int k = 0; k < a.length; k++) {
                    System.out.print(a[k]);
                    if (k != (i - (a.length + 1)))
                        System.out.print(" ");
                }
                System.out.println("");
            }
        }