Nested loop in c programming



A loop inside another loop is known as nested loop. We can write any loop inside any loop in c i.e. we can write for loop inside the loop or while loop or do while loop etc. For example:

(a)
#include<stdio.h>
int main(){
int i,j,k;
     for(i=0;i<3;i++){
         for(j=0;j<3;j++){
             printf(" %d",i+j);
         }
    }
return 0;       
}

(b)
#include<stdio.h>
int main(){
int i,j,k;
    do
         while(0)
             for(;0;)
                 printf("cbyexample");
while(0);
return 0;        
}


For loop
While loop
Do while loop
break and continue
C tutorial home.

82 comments:

keerthi priya said...

write a program to poiny the following format 1
1 1
12 21
123 321
1234 4321
123454321

Rishu gupta said...

how to print
* *
** **
******

Unknown said...

write program for this format
0 0 1 0 0
0 1 0
1

Unknown said...

#include
#include

int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
if(i<=4)
printf(" ");


for(k=i;k>=1;k--)
{
if(k<5)
printf("%d",k);
}

printf("\n");
}
return 0;
}

Unknown said...

#include
#include

int main()
{
int i,j,c,m=5;
for(i=0;i<3;i++)
{
c=m/2;
for(j=0;j<m;j++)
{
if(j==c)
printf("1");
else
printf("0");
}
m=m-2;
printf("\n");

}


return 0;
}

Unknown said...

#include
#include

int main()
{
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
if(i<3)
printf(" ");


for(k=i;k>=1;k--)
{

printf("*");
}

printf("\n");
}
return 0;
}

Anonymous said...

how to program this using nested while loop
*
***
*****
*******
*********
***********

Anonymous said...

how to print
1
21
321
4321
54321

Unknown said...

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

Unknown said...

00100
010
1

Unknown said...
This comment has been removed by the author.
Unknown said...

write a program to print odd and even number between two intervals, which are entered by user?

SAIF AHMED ANIK said...

#include
main()

{
int n,i,j;

scanf("%d",&n);

for (i=0;i<=n;i++)
{
for (j=0;j<=i;j++)
printf("*");
printf("\n");
}

}

Input- 5

SAIF AHMED ANIK said...

WAP this program by using nested loop

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

Anonymous said...

#include
#include
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)
{
printf("*");
}

printf("\n");
}
return(0);
}

Unknown said...

How To Program This

**
**
**
**
**

bosxz said...

Enter outer loop:3
Enter inner loop:5
5 1 4 2 3 3 2 4 1 5
5 1 4 2 3 3 2 4 1 5
5 1 4 2 3 3 2 4 1 5

نُـ‘ـُبُـ‘ـُض آحُـ‘ـُسُـ‘ـُآسُـ‘ـُ said...

Hello can you help me in that's one
1
12
123
1234
12345
1234
123
12
1

Unknown said...

#include
int main()
{
int i,j,n=2;
for(j=0;j<5;j++)
{
for(i=0;i<n;i++)
{
printf("*");
}
printf("\n");
}

return 0;
}

Unknown said...

how to generate the output
N
NE
NEP
NEPA
NEPAL
NEPA
NEP
NE
N

Unknown said...

write a program to print
1
121
12321
1234321
123454321
1234321
12321
121
1

Unknown said...

write it as a rhombus

webdesigners said...



#include
#include
main()
{int i,j;
for(i=1;i<=5;i++)
{for(a=i;a>=i;a--)
{cout<<a;}
cout<<"\n;"}
getch();
}

webdesigners said...

/program in c++/

#include
#include
main()
{
int i,j=1,a=1;
for(i=1;i<=9;i++)
{
while(j<=a)
{cout<4)
a--
else
a++
}
getch();
}

webdesigners said...


#include
#include
main()
{
for(int i=1;i<=9;i++)
{if(i<=5)
{for(int a=i;a<=i;a++)
{cout<=2;)
{b--;cout<<b;}}
else
{a--;
for(c=1;c<a;c++)
{cout<<c;}
for(d=a;d<=2;)
{d--;
cout<<d;}}
}
getch();
}

Unknown said...
This comment has been removed by the author.
Unknown said...

# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
Figure 1

NOTE: Use nested loop/s to write your program

Unknown said...

print
1
01
010
1010

Unknown said...

#include
#include
main()
{
int i,j, n;
printf("Enter the number of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
for(i=1;i<n;i++)
{

for(j=1;j<=n-i;j++)
printf("%d",j);
printf("\n");
}
getch();
}

Danish said...

i wrote a program to print *
***
*****
*******
*********

#include
#include

main()
{
int i,j,k;

for(i=1;i<=5;i=i+2)
{
for(j=1;j<=i;j++)
{
for(k=2;k>=i;k--)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
getch();
}

note: is it correct, reply as soon as possible.

Danish said...

the above program is for stars pyramid. is it correct. reply asap.

Danish said...

i wrote a program to print *
***
*****
*******
*********

#include
#include

main()
{
int i,j,k;

for(i=1;i<=5;i=i+2)
{
for(j=1;j<=i;j++)
{
for(k=2;k>=i;k--)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
getch();
}

note: is it correct, reply as soon as possible.

Baston Sanju said...

WAP to generate given series.
55555
4444
333
22
1

Baston Sanju said...

WAP to generate the given series.
N
NE
NEP
NEPA
NEPAL

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

Write code using nested for loops to display the following output:


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

Unknown said...

Write code using nested for loops to display the following output:


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

Unknown said...

write code using nested loops to show following output:

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

Anonymous said...

public class print1 {
public static void main(String ar[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i+1;j++)
{

System.out.println(j);

}

System.out.println("");
}

}

}

Anonymous said...

this is java programme u can use the logic

Selva Ganesh said...

Anyone can tell me how to print
*****
****
***
**
*

Unknown said...

#include
int main()
{
int i,j;
for (i=1;i<=6;i++)
{
for(j=1;j<=7;j++)
printf("*");
printf("\n");
}
return(0);
}

Unknown said...

how to print
12345
1234
123
12
1

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

how to print
----*
---**
--***
-****
*****

Unknown said...

#include
#include

int main()
{
clrscr();
int a,b,count;
printf("enter the first and last element of the interval");
scanf("%d%d",&a&b);
count=a;
while(count<=b)
{
if(count%2==0)
printf("%d is an even number");
else()
printf("%d is an odd number");
count=count+1;
printf("\n");
}
getch();
return 0;
}

Unknown said...

how to print in c++
*
* *
* * *
* * * *
* * * * *

Unknown said...

int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%d",j);
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}

Unknown said...

*
***
*****
*******
*********
how to generate this output

Unknown said...

That will help you :-)

Unknown said...

#include
#include
int main(void)
{
int y=6;
for(int i=0;i<=4;i++)
{
for(int j=5;j<y;j++)
{
printf("*");
}
y++;
printf("\n");
}
getch();
}

AB said...

How to Display:
12345
678910
11121314151617181920
1617181920

25surivb said...

How to Display: 1 5 2 4 3 3 4 2 5 1 using for, while and do while loop...
I tried but can't get the output. Can some help??? tnx

// the output I'm getting is : 152535455 instead of 1 5 2 4 3 3 4 2 5 1//

#include
#include

int main()

{
int i,j;

for(i=1;i<=5;i++){
for(j=5;j<=5;j++){
printf("%d%d", i,j);
}
}
getch();
}


Jaydeep Zala said...

how to display....
1
212
32123
4321234
543212345

Unknown said...

i want a program that print this :

1 1
1 2
1 3
1 4

2 1
2 2
2 3
2 4

3 1
3 2
3 3
3 4
in double loops

Unknown said...

hi..i am VIKRANT TYAGI

int i,j;

for( i=1; i<=3; i++)
{
for(j=1; j<=4; j++)
{
printf("%d%d\n",i,j);
}
}

Unknown said...

for(i = 1; i <= 5; )
{
for(j = 5; j >= 1; j--)
{
printf("%d %d ", i, j);
i++;

}
}

Unknown said...

#include
#include
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("*");
}
printf("\n");
}getch();
}

Unknown said...

how to print
1
22
333
4444
55555

Unknown said...

how to print
1
22
333
4444
55555

Unknown said...

How to print
1
11
112
1123
11235 plz help me.

Unknown said...

How to print
1
232
34543
4567654

Unknown said...
This comment has been removed by the author.
vallabh said...
This comment has been removed by the author.
vallabh said...

how to print
****
* *
* *
****

Unknown said...

how to get an output
1
2 2
3 3 3
4 4 4 4

Unknown said...

how to get an out put
*
* *
* * *
* * * *
* * * *
* * *
* *
*

Azkarami21 said...

#include
int main()
{
int i=5,j;
while (i >= 1)
{
j=1;
while (j <= i )
{
printf("%d ",j);
j++;
}
printf("\n");
i--;
}
return 0;
}

Unknown said...

How to print this
1
121
12321
1234321

Abc said...

{int I,j;
for(I=1;I<=5;I++);

for(j=1;j<=I;j++);
printf("%d",I);j
printf("\n");}

Abc said...

{int I,j;
for(I=1;I<=5;I++);

for(j=1;j<=I;j++);
printf("%d",I);j
printf("\n");}

Unknown said...

1
121
12321
1234321
plz help me

Unknown said...

#include
int main()
{
int i,j,k,c=1,v=0;
for(i=0;i<4;i++)
{
for(j=3;j>i;j--)
{
printf(" ");
}
for(k=0;k<((2*i)+1);k++)
{
if(k>i)
{
printf("%d",k-(c+v));
c++;
v++;
}

else{
printf("%d",k+1);
}

}
c=1;
v=0;
printf("\n");
}
}

Unknown said...


output:
1
121
12321
1234321

Unknown said...

How to print
5
54
543
5432
54321

Unknown said...

How to print
5
54
543
5432
54321

Aman said...

#include
#include
void main()
{
int i,j;
clrscr();

for (i=1;i<=5;i++)
{
for (j=1;j<=2;j++)
printf("*", j);
printf("\n");
}
getch();
}

sujeet said...

print
*
***
*****
*******

Sports said...
This comment has been removed by the author.