Professional Documents
Culture Documents
Lecture 12
Nested Loop : Activity
• Write a program that reads five numbers (each between 1 and 30).
• For each number read, your program should print a line containing
that number of adjacent asterisks. For example, if your program reads
the number seven, it should print *******.
int input;
cout<<" Enter 4 numbers between 1 and 30 ";
}
int input;
cout<<" Enter 4 numbers between 1 and 30 ";
cin>>input;
if(input>0 && input<=30)
{
for(int j=1; j<= input; j++)
cout<<"*";
}
else
{
cout<<" Incorrect input. Please enter again";
i--;
continue;
}
cout<<endl;
}
Nested Loop
• Tables – outer loop iterates over rows, inner loop deals with columns
in current row
2 4 8 16
3 9 27 81
4 16 64 256
…
Nested Loop
for(int i=1;i<=4;i++)
cout<<i<<" ";
cout<<endl;
for(int i=1;i<=4;i++)
cout<<"x"<<" ";
cout<<endl;
} }
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0)
cout<<"*";
else
cout<<"-";
}
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
else
cout<<"-";
}
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-";
}
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-"; 1 4 -*-*
}
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-"; 1 4 -*-*
} 1 5 -*-*-
cout<< endl;
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-"; 1 4 -*-*
} 1 5 -*-*-
cout<< endl;
2 1 -*-*-
-
}
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-"; 1 4 -*-*
} 1 5 -*-*-
cout<< endl;
2 1 -*-*-
-
} 2 2 -*-*-
-*
for (int i = 1; i<=3; i++)
{ i j output
for(int j=1;j<=5;j++)
{ 1 1 -
if(j%2==0) 1 2 -*
cout<<"*";
1 3 -*-
else
cout<<"-"; 1 4 -*-*
} 1 5 -*-*-
cout<< endl;
2 1 -*-*-
-
} 2 2 -*-*-
-*
2 3 -*-*-
-*-
for (int i = 1; i<=3; i++)
{
for(int j=1;j<=5;j++)
{ Final Output
if(j%2==0) -*-*-
cout<<"*"; -*-*-
else -*-*-
cout<<"-";
}
cout<< endl;
}
for (int i = 1; i<=3; i++)
{
for(int j=1;j<=5;j++)
{
***
if(j%2==0)
**
cout<<"*";
***
else
cout<<” ";
}
cout<< endl;
}
for (int i = 1; i<=5; i++)
{ 1
for(int j=1;j<=(5-i);j++) 22
cout<<" "; 333
4444
for(int k=1;k<=i;k++) 55555
cout<<i;
cout<< endl;
}
What is the output when x = 8?
int x = 0;
cin >> x; 8, 6, 4, 2, 0
while( x%2 == 0 ){ Program ends?
for(int i=x; i >= 0; i -= 2){ What is the output when x = 4?
cout << i << " ";
}
cout << endl; 4, 2, 0
cin >> x;
}
cout << "Done" << endl; What is the output when x = 7?
Done
Activity
Using nested loops write a code that prints the following shape:
*********
*******
*****
***
*
int h = 6;
int row=10;
for(int i=0;i<=h;i++)
{
for(int j=0;j<=row;j++)
cout<<"*";
cout<<endl;
row-=2;
for(int k=0;k<=i;k++)
cout<<" ";