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

Nested Loops C++

The document discusses nested loops in C++. It provides examples of using nested for loops to print various patterns like triangles and squares. The nested loops allow repeating a task multiple times within an ongoing process to generate different patterns by printing characters, numbers or other elements in each iteration.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
426 views

Nested Loops C++

The document discusses nested loops in C++. It provides examples of using nested for loops to print various patterns like triangles and squares. The nested loops allow repeating a task multiple times within an ongoing process to generate different patterns by printing characters, numbers or other elements in each iteration.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
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++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}

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

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

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

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

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

1
22
333
4444
55555

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

55555
4444
333
22
1

int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<i;
}
cout<<"\n";
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<i;
}
cout<<"\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++)
{
cout<<j;
}
cout<<"\n";
}

1
12
123
1234
12345

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

12345
1234
123
12
1

int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<"\n";
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<"\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++)
{
cout<<c;
c++;
}
cout<<"\n";
}

A
AB
ABC
ABCD
ABCDE

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

ABCDE
ABCD
ABC
AB
A

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

A
AB
ABC
ABCD
ABCDE
ABCD
ABC
AB
A
*******************************************
Digital Clock
#include<iostream.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);
cout<<"HH:"<<h<<"MM:"<<m<<"SS:"<<s;
sleep(1);
}
}
}
getch();
}

HH:0 MM:0 SS:0


****************************************
#include<iostream.h>
void main()
{
int i,j,c=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<c;
c++;
}
cout<<"\n";
}
getch();
}

1
23
456
78910
1112131415
***********************************
#include<iostream.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)
{
cout<<"1";
}
else
{
cout<<"0";
}
c++;
}
cout<<"\n";
}
getch();
}

0
10
101
0101
01010
************************************
#include<iostream.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
cout<<j;
}
cout<<"\n";
}
getch();
}

54321
4321
321
21
1
***********************************
#include<iostream.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
cout<<i;
}
cout<<"\n";
}
getch();
}

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

*
* *
* * *
* * * *
* * * * *
**************************************
#include<iostream.h>
void main()
{
int i,j,k,m=5;
clrscr();
for(i=1;i<=5;i++)
{
for(k=m;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
m--;
cout<<"\n";
}
getch();
}

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

You might also like