Hey guys, what is up. In this particular post, we will be talking about the nesting of loops. Also, its working and we will see some questions on the same in which we will be doing pattern questions.

Updated on 29/9/2021

Nesting of loops:

The word nesting means one within another. In the same way nesting of loops is when one loop is under another loop. The inner loop is completely under the outer loop.

How does the nesting of loops works?

In this the counter first executes the outer loop then the counter executed the inner loop until the test expression is true. Then the counter goes to increment or decrement of the outer loop then the inner loop is again executed. These steps are repeated again and again until the test expression of the outer loop is true. Moreover, this process can come to an end if we use a break inside the loop. It is advisable to use for loop in nesting.

Question 1: Write a program to display solid rectangle of “*”.

output 1 nesting of loops c

#include<iostream.h>

int main()

{

            int rows, columns;

            int i,j; // i and j are variables for 2 loops   

//Taking input of rows and columns as to get dimensions of rectange

            cout<<“ENTER NO. OF ROWS= ”;

            cin>>rows;

            cout<<endl<<“ENTER NO. OF COLUMNS= ”;

            cin>>columns;

            for(i=1;i<=rows;i++)  //outer loop for rows

            {

                        for(j=1;j<=columns;j++) //inner loop for columns and for printing

                        {

                                    cout<<“*”;

                        }

                        cout<<endl;

            }

            return 0;

}

Question 2: Write a program to display upper half pyramid using stars.

output 2 nesting of loops

#include<iostream.h>

int main();

{

            int i,j,n;           // n is for number of rows

            cout<<“ ENTER NUMBER OF ROWS= ”;

            cin>>n;

            for(i=1; i<=n; i++)      //outer loop for row

            {

                        for(j=1; j<=i; j++)        //inner loop for printing of star

                        {

                                    cout<<“*”;

                        }

                        cout<<endl;

            }

            return 0;

}

Question 3: Write a program to display the following pattern using nesting of loops.

output 3 nesting

#include<iostream.h>

int main();

{

            int i,j,n;           //i and j for loops and n for number of rows

            cout<<“ENTER THE NO. UPTO WHICH YOU WANT THE PYRAMID =”;

            cin>>n;

            for(i=1;i<=n;i++)        // outer loop

            {

                        for(j=1;j<=i;j++)          //inner loop

                        {

                                    cout<<j;

                        }

                        cout<<endl;

            }

            return 0;

}

Question 4: Write a program to display the pattern using numbers.

output 4

#include<iostream.>

int main();

{

            int i,j,n;

            cout<<“ ENTER THE VALUE OF N= ”;

            cin>>n;

            for(i=1;i<=n;i++)

            {

                        for(j=1;j<=i;j++)

                        {

                                    cout<<i;

                        }

                        cout<<endl;

            }

            return 0;

}

Note: We can also make these programs using do and while loop.

For writing the above program in a while loop we will copy all the steps up to the statement of taking n from the user. Then we will give the value of i and j equal to 1. In place of for loop, we will write while(i<=n) then we will go in the inner loop there we will write while(j<=i) and in braces, we will print i. In the body of the inner loop, we will write j++ that is our increment condition. Then we will close the braces of the inner loop and in the body of the outer loop, we will write i++.

You can solve these questions using while and do while you can ask your doubts in the comment section. Or you can message us on Instagram also.

Question 5: Write a program to display the pattern using stars.

output 5

#include<iostream.h>

int main();

{

            int i,j,n;

            cout<<“ENTER THE NO. OF ROWS= ”;

            cin>>n;

            i=n;

            while (i>=1)

            {

                        j=i;

                        while (j>=1)

                        {

                                    cout<<“*”;

                                    j–;

                        }

                        cout<<endl;

                        i–;

            }

            return 0;

}          

In the upcoming post, you will be getting information related to data science and array in c++. If you haven’t read about what are loops and its syntax then go and read this. You can read previous posts by scrolling downwards. Also, stay tuned for the upcoming post.


1 Comment

Raghav · May 5, 2021 at 10:14 am

Sexy code helped alot ..

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *