Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range ‘a’ through last.

1
Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range ‘a’ through last.

LANGUAGE: C++

CHALLENGE:

Given an char variable last that has been initialized to a lowercase letter, write a loop that displays all possible combinations of two letters in the range ‘a’ through last. The combinations should be displayed in ascending alphabetical order:

For example, if last was initialized to ‘c’ the output should be aa
ab
ac
ba
bb
bc
ca
cb
cc

SOLUTION:


for (char outterChar='a'; outterChar <=last; outterChar++){
    for (char innerChar='a'; innerChar <=last; innerChar++){
        cout << outterChar << innerChar << "\n";
    }
}

One comment

  • Ben Large says:

    Here is the C version:

    char outterChar;
    char innerChar;

    for (outterChar=’a’; outterChar <=last; outterChar++)
    for (innerChar='a'; innerChar <=last; innerChar++)
    printf("%c%c\n",outterChar, innerChar);

Add comment