0

i have to print specific pattern using Java for loop . Input will be 4 characters eg.

    a, b , c, d  

now what i have to print is

    aaaa
    aaab
    aaac
    aaad
    aaba
    abbb
    aabc

And so on.. i have tried some pyramids codes in java and understand to code nested for loops , but really getting no idea how to print this , Please guide me how i can achieve this . Thank You

9
  • 5
    Show effort -> ask question -> get an answer. Commented Apr 30, 2013 at 18:38
  • 2
    Look at the bright side: if you System.out.println() those one by one, it's only 256 lines. Totally doable.
    – iluxa
    Commented Apr 30, 2013 at 18:40
  • @whoAmI if i m not getting idea to do it then should i paste irreverent codes?
    – Sikander
    Commented Apr 30, 2013 at 18:43
  • @Sikander if you want us to write code for you - it won't happen. If you want us to teach you Java - also, not gonna happen. If you ask a specific question - we will try very hard to help you! Commented Apr 30, 2013 at 18:46
  • 1
    @whoAmI: he just ask to guide him :) Commented Apr 30, 2013 at 18:48

2 Answers 2

1

Count from 0 to nn-1, where n is the number of characters. For each count value, determine its base-n representation. Pad to the left with zeroes up to width n. Use each digit as an index into the characters. Print.

1

If you absolutely must use loops, you can nest some loops

for(char c1 = 'a'; c1<= 'd'; c1++)
{
    for(char c2 = 'a'; c2<= 'd'; c2++)
    {
        for(char c3 = 'a'; c3<= 'd'; c3++)
        {
            for(char c4 = 'a'; c4<= 'd'; c4++)
            {
                //print some combination of c1, c2, c3, and c4
            }
        }
    }
}

If your inputs aren't contiguous chars, than your loops might look like

char[] inputs
//initialize inputs
for(int i1 = 0; i1 < inputs.length; i++)
{
    char c1 = inputs[i1];
    ...
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.