0% found this document useful (0 votes)
334 views

Nested Loops C

A nested loop is used to repeat a task multiple times within an ongoing process. It contains one or more loops within another loop. The document provides examples of using nested for loops to print various patterns like triangles and numbers. Code snippets are included to demonstrate how nested loops can be used to print patterns, count up and down, and display a digital clock by nesting loops that iterate through hours, minutes, and seconds.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
334 views

Nested Loops C

A nested loop is used to repeat a task multiple times within an ongoing process. It contains one or more loops within another loop. The document provides examples of using nested for loops to print various patterns like triangles and numbers. Code snippets are included to demonstrate how nested loops can be used to print patterns, count up and down, and display a digital clock by nesting loops that iterate through hours, minutes, and seconds.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Nested Loop

One or more Loop within another loop, the loop may be for, while or do while and it
is used to repeat a task a given number of times within one ongoing process,
basically it is used to print different triangular or similar patterns initially

int i,j;
for(i=1;i<=5;i++) Outer Loop - Starts first - counts number of lines
{
for(j=1;j<=i;j++) Inner Loop - Finishes first - counts number of items in each line
{
printf("*");
}
printf("\n");
}

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

i=1
j=1 to 1
i=2
j=1 to 2
i=3
j=1 to 3
i=4
j=1 to 4
i=5
j=1 to 5

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

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

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

*
**
***
****
*****
****
***
**
*
--------------------------------------------
int i,j;
for(i=1;i<=5;i++) Counts number of lines
{
for(j=1;j<=i;j++) Counts number of items in each line
{
printf("%d",i);
}
printf("\n");
}

1
22
333
4444
55555

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

55555
4444
333
22
1

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

1
22
333
4444
55555
4444
333
22
1
--------------------------------------------
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}

1
12
123
1234
12345

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

12345
1234
123
12
1

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

1
12
123
1234
12345
1234
123
12
1
--------------------------------------------
int i,j;
char c='A';
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",c);
c++;
}
printf("\n");
}

A
AB
ABC
ABCD
ABCDE

int i,j;
char c='A';
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%c",c);
c++;
}
printf("\n");
}

ABCDE
ABCD
ABC
AB
A

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

A
AB
ABC
ABCD
ABCDE
ABCD
ABC
AB
A
*****************************************
Digital Clock
#include<stdio.h>
void main()
{
int h,m,s;
for(h=0;h<24;h++)
{
for(m=0;m<60;m++)
{
for(s=0;s<60;s++)
{
clrscr();
gotoxy(40,12);
printf("HH:%d MM:%d SS:%d",h,m,s);
sleep(1);
}
}
}
getch();
}
****************************************
#include<stdio.h>
void main()
{
int i,j,c=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%3d",c);
c++;
}
printf("\n");
}
getch();
}

1
23
456
78910
1112131415

***********************************
#include<stdio.h>
void main()
{
int i,j,c=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(c%2==0)
{
printf("1");
}
else
{
printf("0");
}
c++;
}
printf("\n");
}
getch();
}

0
10
101
0101
01010
************************************
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
54321
4321
321
21
1
***********************************
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

55555
4444
333
22
1
*******************************
#include<stdio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i+j);
}
printf("\n");
}
getch();
}
*********************************
#include<stdio.h>
void main()
{
int i,j,k,m=5;
clrscr();
for(i=1;i<=5;i++)
{
for(k=m;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf(" *");
}
m--;
printf("\n");
}
getch();
}

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

You might also like