IN JAVA: Output all combinations of character variables a, b, and c, using this
ID: 3874822 • Letter: I
Question
IN JAVA: Output all combinations of character variables a, b, and c, using this ordering:
abc acb bac bca cab cba
So if a = 'x', b = 'y', and c = 'z', then the output is: xyz xzy yxz yzx zxy zyx
Your code will be tested in three different programs, with a, b, c assigned with 'x', 'y', 'z', then with '#', '$', '%', then with '1', '2', '3'.
public class OutputCombinations {
public static void main (String [] args) {
char a;
char b;
char c;
a = 'x';
b = 'y';
c = 'z';
/* Your solution goes here */
System.out.println("");
}
}
Explanation / Answer
class Main {
public static void main (String [] args) {
char a;
char b;
char c;
a = 'x';
b = 'y';
c = 'z';
/* Your solution goes here */
String st = "";
st += a;
st +=b;
st +=c;
// looping for first character
for(int i=0; i<3; i++)
{
// looping for second character
for(int j=0; j<3; j++)
{
// looping for third character
for(int k=0; k<3; k++)
{
// when no number is same as other
if(i!=j && j!=k && i!=k)
{
// printing combination
System.out.printf("%c%c%c ",st.charAt(i),st.charAt(j), st.charAt(k));
}
}
}
}
System.out.println("");
}
}
/*Outputs
xyz xzy yxz yzx zxy zyx
#$% #%$ $#% $%# %#$ %$#
123 132 213 231 312 321
*/