Explain about Nested Loops with Examples in C language
When we are placing a loop within the loop body, then it is called Nested loop.
In implementation, when we require to repeat block of the statement, then go for loop, if complete loop body, if we required to repeat “n” no of times then go for nested loops. ?
Syntax :-
Assignment;
While(condition1)
{
Statement 1;
Statement 2;
-----------------
Asignment2;
While(condition2)
{
Statement 3;
Statement 4;
…………………..
…………………..
Include;
}
…………………….
Include;
}
- When we are working with the nested loops, always execution process will starts from outerloop condition. Ie condition1
- Whenever the outerloop condition is true, then control will pass within the outerloop.
- In order to execute the outerloop, if any while statements are occur, those are called “innerloop”
- Whenever the innerloop is occur, then we required to check inner loop condition i.e, condition 2
- If the innerloop condition is true then control will pass within the innerloop and until the inner loop condition became false, complete body will be repeated “n” no. of times.
- Whenever the inner loop condition is false, then control will pass to outer loop and until the outer loop condition is false, it will be repeated “n” no. Of times
- Enter two values : 2 4
2*1=22*2=42*3=62*4=82*5=102*6=12 2*7=14 2*8=16 2*9=18 2*10=20
| 3*1=33*2=63*3=93*4=123*5=153*6=18 3*7=21 3*8=24 3*9=27 3*10=30
| 4*1=44*2=84*3=124*4=164*5=204*6=24 4*7=28 4*8=32 4*9=36 4*10=40
|
Void main()
{
Int i,n,n1,n2;
Clrscr();
Printf(“Enter two values”);
Scanf(“%d%d”,&n1,&n2); n1 n2
I=1; 2 4
While(i<=10) n as n=n1
{ checkout conditions
Printf(“\n”);
N=n1;
While(n<=n2)
{
Printf(“%3d%2d=%2d”,n,i,n,i);
N=n+1;
}
I=i+1;
}
Getch();
Enter a value : 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Enter a value : 6
6 5 4 3 2 1
5 4 3 2 1
4321
321
21
1
Void main(){Int i,in,n;Clrscr();Printf(“enter a value;”);Scanf(“%d”,&n); I=1; While (i<n) { Printf(“\n”); In=1; While(in<=i) { Printf(“%d”,in); In=in+1; } I=i+1; } Getch(); }
| 123456234563456456566
654321 65432 6543 654 65 6
|
Void main()
{
Int i,n,dn;
Clrscr();
Printf(“Enter a value”);
Scanf(“%d”,&n);
I=n;
While(i>=1)
Printf(“\n”);
Dn=i;
While(dn>=i)
{
Printf(“%d”,dn)
Dn=dn-1;
I=i-1;
}
Getch();
}
o/p : 65431
54321
4321
321
21
1
Voidmain()
{
Int i,n,in;
Clrscr();
Printf(" Enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=0)
Printf(“\n”);
In=i;
While(in<=n)
{
Printf(“%d”,in);
In=in+1;
}
I=i+1;
Getch();
}
o/p : Enter a value :6
1234356
23456
3456
456
56
6
Void main()
{
Int i,n,dn;
Clrscr();
Printf("Enter a value:”);
Scanf(“%d”, &n);
I=n;
While(i<=n)
{
Printf(“\n”);
Dn=n;
While(dn>=i)
{
Printf(“dn”,dn);
Dn=dn-1;
}
I=i+1;
}
Getch();
}
o/p:- 654321
65432
6543
654
65
6
Void main()
{
Int i,n,s,in;
Clrscr();
Printf(“Enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
S=1;
While(s<=n-1)
{
Printf(“*”);
S=s+1;
}
In=1;
While(in<=i)
{
Printf(“%d”,in);
In=in+1;
}
I=i+1;
{
Getch();
}
o/p : enter a value : 6
***** 1
***** 2
***123
***234
***345
123456
Void main()
{
Printf(“i,n,in”);
Clrscr();
Printf(“enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
In=1;
While(in<=i)
{
Printf(“%d”,i);
In=in+1;
}
I=i+1;
}
Getch();
}
o/p:
1
22
333
444
5555
66666
Void main()
{
Int i,in,n;
Clrscr();
Printf(“enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
In=1;
While(in<=i)
{
If(i%2==0)
Printf(“*”);
Else
Printf(“%d”,in);
In=in+1;
}
I=i+1;
}
Getch();
}
o/p :
1
**
123
****
12345
*****
Void main()
{
Int i,in,n;
Clrscr();
Printf(“enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
In=1;
While(in<=i)
{
If(i%2==0 && in%2!=0)
Printf(“*”);
Else if (i%2!=0 && in%2==0)
Printf(“*”);
Else
Printf(“%d”,in);
In=in+1;
}
I=i+1;
}
Getch();
}
o/p:
1
*2
1*3
*2*4
1*3*5
*2*4*6
Void main()
{
Int i,in,n;
Clrscr();
Printf(“enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
In=1;
While(in<=i)
{
If(in%2==0)
Printf(“*”);
Else
Printf(“%d”,in);
In=in+1;
}
I=i+1;
}
Getch();
}
o/p :
1
1 *
1 * 3
1 * 3 * 5
1 * 3 * 5 *
Void main()
{
Int i,in,n,k;
Clrscr();
Printf(“enter a value”);
Scanf(“%d”,&n);
I=1;
K=1;
While(i<=n)
{
Printf(“\n”);
In=1;
While(in<=i)
{
Printf(“%3d”,k);
In=in+1;
K=k+1;
}
I=i+1;
}
o/p:
1
2 3
456
78910
1112131415
1617181920
Void main()
{
Int i,n,s,dn;
Clrscr();
Printf(“Enter a value”);
Scanf(“%d”,&n);
I=1;
While(i<=n)
{
Printf(“\n”);
S=1;
While (s<=i-1)
{
Printf(“*”);
S=s+1;
}
Dn=n;
While(dn>=1)
{
Printf(“%d”,dn);
Dn=dn-1;
}
I=i+1;
}
Getch();
}
o/p:
654321
*65432
**6543
***654
****65
*****6
Void main()
{
Int i,n,s,dn,in;
Clrscr();
Printf(“Enter a value”);
Scanf(“%d”,&n);
I=n;
While(i>=1)
{
Printf(“%d”,in);
In=in+1;
}
S=1;
While(s<=(i-1)*2)
{
Printf(“*”);
S=s+1;
}
Dn=i;
While(dn<=n)
{
Printf(“%d”,dn);
Dn=dn+1;
}
I=i-1;
}
Getch();
}
o/p: 1 * * * * * * * * * * 6
1 2 * * * * * * * * 56
1 2 3 * * * * * * 4 5 6
1 2 3 4 * * * * 3 4 5 6
1 2 3 4 5 * * 2 3 4 5 6
1 2 3 4 5 6 1 2 3 4 5 6
Voidmain()
{
Int i,n,in,dn,s;
Clrscr();
Pritntf(“Enter a value”);
Scanf(“%d”,%n);
I=n;
While(i>=1)
{
Printf(“\n”);
S=1;
While(s<i-1)
{
Printf(“*”);
S=s+1;
}
Dn=n;
While(dn>=1)
{
Printf(“%d”,dn);
Dn=dn-1;
}
in=i+1;
While(in<=n)
{
Printf(“%d”,in);
In=in+1;
}
I=i-1;
}
Getch();
}
Void main()
{
Int i,n,in,dn,s;
Clrscr();
Printf(“Enter a value”);
Scanf(“%d”,&n);
I=1;
Which(i<=n)
{
Printf(“\n”);
S=1;
While(s<=n-1)
{
Printf(“*n”);
S=s+1;
}
In=1;
While(in<=1)
{
Printf(“%d”,in);
In=in+1;
}
Dn=i-1;
While(d>=1)
{
Printf(“%d”,dn);
Dn=dn-1;
}
I=i+1;
}
In=1;
While(in<=i)
{
Printf(“%d”,in)
In=in+1;
}
Dn=i-1;
While(dn>=1)
{
Printf(“%d”,dn);
Dn=dn-1;
}
I=i+1;
I=1;
While(i<=n-1)
{
Printf(“\n”);
S=1;
While(s<=i)
{
Printf(“*”);
S=1;
While(s<=i)
{
Printf(“*”);
S=s+1;
}
In=1;
While(in<=n-1)
{
Printf(“%d”,in);
In=in+1;
}
Dn=n-i-1;
While(dn>=1)
{
Printf(“%d”,dn)
Dn=dn-1;
}
I=i+1;
}
Getch();
}