Manual # 6
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;
}
#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:
-----------------------------------------------------------------------------------------------------------
-
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.
-----------------------------------------------------------------------------------------------------------
-