0

# Can anyone please explain these loops?

1 Answer

+ 2

for (int i = 0;i <= 9; i++) {
for (int d = 10 - i; d > 0;d--) {
cout<<"\t";
}
for (int j = 1; j <= i; j++) {
cout<<"\t"<<i<<"\t" ;
}
cout<<"\n";
}
when i=0,
1st nested loop continues 10 times and so it gives 10 tab spaces.
in the 2nd nested loop j=1 and i=0 so loop not works.
i=1 and 1st nested loop continues 9 times and so it gives 9 tab spaces.
In the 2nd nested loop j=1 and i=1 so the loop continues 1 times and it
prints 1 tab space then 1 and then 1 tab spaces.
i=2 and 1st nested loop continues 8 times and so it gives 8 tab spaces.
In the 2nd nested loop j=1 and i=2 so the loop continues 2 times and it
prints 1 tab space then 2 and then 1 tab spaces and again 1 tab space and then 2 and 1 tab space.
i=3 and 1st nested loop continues 7 times and so it gives 7 tab spaces.
In the 2nd nested loop j=1 and i=3 so the loop continues 3 times and it
prints 3, 3 times where the difference between these numbers are two tab spaces.
This continues.......
when i=9, 1st nested loop continues 1 times and so it gives 1 tab spaces.
In the second nested loop j=1 and i=9 so the loop continues 9 times and it prints 9 9 times where the difference between these numbers are two tab spaces.
And then the loop stop .
Hope this helps.

Hot today

Class and instance attributes

1 Votes

How does this works?

1 Votes

Please help

1 Votes

Button after Button

0 Votes

FYP PROJECT IDEAS

0 Votes