CSE150 Lecture Notes 05
CSE150 Lecture Notes 05
(C++ Programming)
~Lecture Notes 05~
Prepared
by: Dr. Lamiaa Abdel-Hamid
Presented Dr. Mayada Osama
by: Eng. Esraa Nashaat
Nested loops
2
Example 1
What will be the output of the following
C++ code?
3
Example 1 (soln)
What will be the output of the following
C++ code?
4
Example 2
What will be the output of the following
C++ code?
5
Example 2 (Soln)
What will be the output of the following
C++ code?
6
Example 3
Write a C++ program that prints the
following patterns using loops and only
a single cout statement : cout<< "* ";
7
Example 3
Write a C++ program that prints the
following patterns using loops and only
a single cout statement : cout<< "* ";
for (int r=1;r<=5;r+
for (int r=1;r<=4;r++)
+)
{
{
for (int c=1;c<=7;c+
for (int
+)
c=1;c<=r;c++)
{ cout<<"*";
{
cout<<"*";
}
}
cout<<endl;
cout<<endl;
}
}
8
Example 4
What will be the output of the following
C++ codes:
a) b)
int i, j; int i,j,x=0;
9
Example 4 (SOLN)
What will be the output of the following
C++ codes:
a) b)
int i, j; int i,j,x=0;
10
Examples
11
EXAMPLE 5
Q1.
Write a C++ program that prints the
following pattern:
********
******
****
**
EXAMPLE 5 SOL
Sol . Another Sol
#include #include <iostream>
<iostream> using namespace std;
int main()
using namespace
{
std; for(int r=4;r>=1;r--)
int main() { {
for(int for(int c=1;c<=2*r; c++)
r=8;r>=2;r-=2) {
cout << "*";
{
}
for(int c=1;c<=r; cout<<endl;
c++) }
{ return 0;
cout << "*"; }
}
EXAMPLE 6
**
******
******
************
**********
EXAMPLE 6 SOL
Sol
else
#include <iostream> {
using namespace std; for(int c=1;c<=2*r; c++)
int main() {
{ cout << "*";
for(int r=1;r<=5;r++) }
{ cout<<endl;
if (r%2 ==0) }
{ }
for(int c=1;c<=3*r; c++) return 0;
{ }
cout << "*";
}
cout<<endl; }
EXAMPLE 7
19