0% found this document useful (0 votes)
57 views8 pages

Manual # 6

This document is a manual on nested for loops and diamond patterns in C++ programming. It contains 7 questions asking the reader to write programs that output various diamond and nested patterns using nested for loops. The manual provides examples of nested for loops where the inner loop is affected by the outer loop counter in different ways. It also explains when initializing an inner loop counter using an uninitialized outer counter will cause garbage output.

Uploaded by

musa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views8 pages

Manual # 6

This document is a manual on nested for loops and diamond patterns in C++ programming. It contains 7 questions asking the reader to write programs that output various diamond and nested patterns using nested for loops. The manual provides examples of nested for loops where the inner loop is affected by the outer loop counter in different ways. It also explains when initializing an inner loop counter using an uninitialized outer counter will cause garbage output.

Uploaded by

musa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Manual # 6

Topic:
Nested For Loops + Diamond
Programming

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
-----------------------------------------------------------------------------------------------------------
Note the behavior of the program.
Nested For Loop: No Effect of major for loop’s variable ‘i’ on secondary for loop. (i.e.
for(j=0;j<=10;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=0;j<=10;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Nested For Loop: Effect of major for loop’s variable ‘i’ on initialization of secondary for
loop. (i.e. for(j=i+1;j<=10;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
cout<<i;

for(j=i+1;j<=10;j++)
{
cout<<j;
}

cout<<"\n";

2
}

cout<<"\n\n";

return 0;
}

The above program can also be written as:

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=i;j<=10;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Nested For Loop: Effect of major for loop’s variable ‘i’ on condition of secondary for
loop. (i.e. for(j=0;j<=i;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=0;j<=i;j++)
{
cout<<j;
}

3
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Nested For Loop: Effect of major for loop’s variable ‘i’ on initialization & condition of
secondary for loop. (i.e. for(j=i;j<=i;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=i;j<=i;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Nested For Loop: Effect of major secondary loop’s variable ‘j’ on initialization of
secondary for loop. (i.e. for(i=j;j<=10;i++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=j;i<=10;i++)
{

4
for(j=i;j<=i;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}

Note: You will see the garbage on the output. It is because of the reason that you did not
assign any value to j and you make i=j, which means that you are assigning the value j to
I, but you did not assign any value to j, so that’s why this kind of programming will not
work.
-----------------------------------------------------------------------------------------------------------
-
Q1: Write a program which will take the value from a user between 0 to 25, you program
should display the sequence of numbers to the value entered by the user in such a way:

i.e. the value entered is 14, starting from 0 to the


value entered by the user in such a way that
if the value is even that display the sequence
of the value to the value entered by the user
else if the value is odd just display the value
as clear from the figure.

-----------------------------------------------------------------------------------------------------------
-
Write a program which will create the output as show below:

5
#include <iostream.h>
#include<math.h>

int main()
{
int i,j;

for(i=0;i<8;i++)
{
for(j=0;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Q2: Write a program which will create the output as shown below:

-----------------------------------------------------------------------------------------------------------
-
Q3: Write a program which will create the output as shown below:

6
-----------------------------------------------------------------------------------------------------------
-

Q4: Write a program which will create the output as shown below:

-----------------------------------------------------------------------------------------------------------
-
Q5: Write a program which will create the output as shown below:

-----------------------------------------------------------------------------------------------------------
-
Q6: Write a program which will create the output as shown below:

7
-----------------------------------------------------------------------------------------------------------
-

Q7: Write a program which will create the output as shown below:

Note: You can combine Answers of Q4 & Q5, to get the output as shown above.
-----------------------------------------------------------------------------------------------------------
-

You might also like