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 = 1; j <= 4; j++) { for (int i = 1; i <= 5; i++) { cout << i; } cout << endl; }}12345
12345
12345
123452. WAP to print following output
#include <iostream>
using namespace std;main(){ #include <iostream>using namespace std;
main(){
for (int n = 1; n <= 5; n++) { for (int m = 1; m <= n; m++) { cout << m; } cout << endl; }}Output:
1
12
123
1234
12345
Comments
Post a Comment