Nested For Loops + Diamond Programming


Manual # 6

Topic:

Nested For Loops + Diamond Programming

Subject:

Introduction to Computing

Author:
Sunawar Khan Ahsan

-----------------------------------------------------------------------------------------------------------

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

            }



            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;

                        }

                     

                        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++)

            {

                        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:





















#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:



















------------------------------------------------------------------------------------------------------------





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:



















------------------------------------------------------------------------------------------------------------





















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.

------------------------------------------------------------------------------------------------------------

Previous Post Next Post