Ticker

6/recent/ticker-posts

C Language (Nested Loop)

NESTED FOR LOOP IN C
When one or more loops define inside the boundary of other loops then it's known as nested loop.


A Simple Example of Nested Loop
for(i=0; i<4; i++)         // Outer loop
{
for(j=0; j<6; j++)                     // inner loop
{

}           // close inner loop

}           //  close outer loop



1
1 2
1 2 3
1 2 3 4
1 2 3 4 5


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1



A
A B
A B C
A B C D
A B C D E



*
* *
* * *
* * * *
* * * * *



1
2 2
3 3 3
4 4 4 4
5 5 5 5 5


#include<stdio.h>
#include<conio.h>
Void main()
{                                                                                              
clrscr();
int i,k;
for(i=0; i<5; i++)
{
printf(”\n”);
for(k=1; k<=5-i; k++)            
{
printf(“%d”,k);
}
}
getch();
}

---Output---

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include<stdio.h>


#include<conio.h>
Void main()
{
clrscr();
int i,k;
for(i=1; i<=5; i++)
{
printf(”\n”);
for(k=1; k<=i; k++)
{
printf(“*”);
}
}
getch();
}

---OUTPUT---
*
* *
* * *
* * * *
* * * * *

#include<stdio.h>
#include<conio.h>
void main()
{



clrscr();
int i, j, k, s=3;
for(i=1; i<=7; i=i+2)
{
for(k=s; k>0; k--)
{
printf(“ “ );
}
for(j=1; j<=i; j++)
{
printf(“*”);
}
printf(“\n”);
s=s-1;
}
getch();
}


---OUTPUT---




*
* * *
* * * * *
* * * * * * *










Post a Comment

0 Comments