C++ 12: Nested Loops

A loop within another loop is called a nested loop. Let's take an example,

Suppose we want to loop through each day of a week for 3 weeks.

To achieve this, we can create a loop to iterate three times (3 weeks). And inside the loop, we can create another loop to iterate 7 times (7 days). This is how we can use nested loops.


 

WAP a program to print following output:

12345

12345

12345

12345

#include <iostream>
using namespace std;
main()
{
    #include <iostream>
using namespace std;

main()
{

    for (int j = 1j <= 4j++)
    {
        for (int i = 1i <= 5i++)
        {
            cout << i;
        }
        cout << endl;
    }
}
12345
12345
12345
12345

2. WAP to print following output 

#include <iostream>
using namespace std;
main()
{
    #include <iostream>
using namespace std;

main()
{

   for (int n = 1n <= 5n++)
    {
        for (int m = 1m <= nm++)
        {
            cout << m;
        }
        cout << endl;
    }
}

Output:

1
12
123
1234
12345























































Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected