1
+ // You can see explanation of program in last of the program see its important
2
+ #include < iostream>
3
+ using namespace std ;
4
+
5
+ int main ()
6
+ {
7
+ int n;
8
+ cout<<" Enter how many lines : " ;
9
+ cin>>n;
10
+ for (int i=1 ;i<=n;i++)
11
+ {
12
+ for (int j=1 ;j<=i;j++)
13
+ {
14
+ cout<<i<<" " ; // if you print j here then series will be 1\n 1 2\n 1 2 3\n 1 2 3 4 like this
15
+ }
16
+ cout<<endl;
17
+ }
18
+
19
+ return 0 ;
20
+ }
21
+
22
+ /*
23
+ On execution we input ythe number of lines n (say 4).
24
+ In this program the inner j loop is nested inside the outer i loop.
25
+ For each value of variable i of outer loop, the inner loop will be executed completely.
26
+ For 1st iteration of outer loop, the inner loop will be executed completely.
27
+ For 1st iteration of outer loop, the i is initialized to 1 and inner loop is executed once as the condition (j<=i) ,
28
+ is satisfied only once, thus prints the value of i (i.e. 1) once.
29
+ the statement cout<<"\n" must be outside the inner loop and inside the outer loop in order to produce exactly one
30
+ line for each iteration of outer loop.
31
+ On second iteration(pass) of outer loop, when i=2 then inner loop executes 2 times and thus displaying value of i (i.e 2)
32
+ 2 times and process will continue until the condition in the loop becomes false.
33
+ */
0 commit comments