You are on page 1of 22

CS 100

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 ";

for(int i=1; i<=5;i++)


{
cin>>input;
for(int j=1; j<= input; j++)
cout<<"*";
cout<<endl;

}
int input;
cout<<" Enter 4 numbers between 1 and 30 ";

for(int i=1; i<=5;i++)


{

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

Q – how to print a table with powers of xn for x is 1 to 10


output: (when n is till 4)
x 1 x 2 x 3 x4
1 1 1 1

2 4 8 16

3 9 27 81

4 16 64 256


Nested Loop

Print table header


For x from 1 to 10
for n from 1 to 4
print xn
print endl;
int n;

for(int i=1;i<=4;i++)
cout<<i<<" ";
cout<<endl;
for(int i=1;i<=4;i++)
cout<<"x"<<" ";
cout<<endl;

for (int i = 1; i<10;i++)


{

for(int j=1; j<=4; j++ )


cout<<pow(i,j)<<" ";
cout<<endl;
}
for (int i = 1; i<=3; i++) for (int i = 1; i<=4; i++)
{ {
for(int j=1;j<=4;j++) for(int j=1;j<=i;j++)
cout<<"*"; cout<<"*";

cout<< endl; cout<< endl;

} }

for (int i = 1; i<=4; i++)


{
for(int j=1;j<=3;j++)
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)
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<<" ";

You might also like