0% found this document useful (0 votes)
10 views19 pages

CSE150 Lecture Notes 05

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

CSE150 Lecture Notes 05

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

Introduction to

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

for (i = 0; i < 3; i++) for (i=1;i<=3;i++)


{ {
for (j = 0; j <5; j++) for (j=2;j<=4;j++)
cout << " " << i; {
x=x+j;
cout << endl; }
} cout<<"x = "<<x<<endl;
}

9
Example 4 (SOLN)
What will be the output of the following
C++ codes:
a) b)
int i, j; int i,j,x=0;

for (i = 0; i < 3; i++) for (i=1;i<=3;i++)


{ {
for (j = 0; j <5; j++) for (j=2;j<=4;j++)
cout << " " << i; {
x=x+j;
cout << endl; }
} cout<<"x = "<<x<<endl;
}

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

Write a C++ program that prints the


following pattern:

**
******
******
************
**********
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

Write a C++ program that prints the


following pattern:
MMMM
MMM
MM
M
N
NN
NNN
NNNN
EXAMPLE 7 SOL
Sol

#include <iostream> for (int r1=1;r1<=4;r1++)


using namespace std; {
int main() for (int c1=1;c1<=r1;c1++)
{ {
for (int r=4;r>=1;r--) cout << "N";
{ }
for (int c=1;c<=r;c++) cout << endl;
{ }
cout << "M"; return 0;
} }
cout << endl;
}
EXAMPLE 7 SOL
ANOTHER Sol
else
#include <iostream> {
using namespace std; for(int c=1;c<=r-4; c++)
int main() { {
int m=4; cout << "*";
for(int r=1;r<=8;r++) }
{ cout<<endl;
if (r<=4) }
{ }
for(int c=1;c<=m; c++) return 0;
{ }
cout << "*";
}
cout<<endl;
m-=1;
}
Example 8

Write a C++ program that :


• Continuously asks the user if he wants to
continue.
• If yes, ask the user to enter N
• the program then reads positive numbers
from the user
• The program then computes the average of
the numbers divisible by 3 and 5.

19

You might also like