codecodecode codecodecode - 1 month ago 19
C++ Question

Creating a for loop to continue to count

what would be the code to continue counting with a for loop? like if i wanted put in a char and have it print once on the first line, twice on the second line, third on the third line and so on?

I've tried

for (int a; a<5; a++)
cout "L";


but this only prints one L per line

i need it to be more like

L
LL
LLL

Answer

You need an additional loop if you do not want to use something like std::string. The inner loop will add L with it's respective amount per line to the output. Once all the amount of Ls are written, you end the line.

Something like:

int main() {

    for (int a = 1; a < 5; a++)
    {
        for (int l = 1; l <= a; l++)
        {
            std::cout << "L";
        }
        std::cout << std::endl;
    }

    return 1;
}
Comments