Lecture-7 [Nested Loops with examples]

Pattern 1


/*
*
**
***
****
*****
******
*******
********
*********
**********
*/

#include <iostream>
using namespace std;
int main()
{

    printf("Enter Number Of Row : ");
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}


Pattern 2

  /* 
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

*/



#include <iostream>
using namespace std;
int main()
{
    printf("Enter Number Of Row : ");
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <=n; j++)
        {
            if (j <= n - i)
            {
                printf(" ");
            }
            else
            {
                printf("*");
            }
        }

        printf("\n");
    }

    return 0;
}


Pattern 3

/*
1
22
333
4444
55555
666666
7777777
88888888
999999999
*/


#include <iostream>
using namespace std;
int main()
{
    printf("Enter Number Of Row : ");
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << i;
        }
        printf("\n");
    }
    return 0;
}




Pattern 4

/*
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

*/


#include <iostream>
using namespace std;
int main()
{
    printf("Enter Number Of Row : ");
    int n;
    cin >> n;
    for (int i = 1, k = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << k<< " ";
            k++;
        }
        printf("\n");
    }

    return 0;
}


Pattern 5

/*
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

*/


#include <iostream>
using namespace std;
int main()
{
    printf("Enter Number Of Row : ");
    int n;
    cin >> n;

    for (int i = 1, k=0; i <= n; i++, k=0)
    {
        for (int j = 1; j <= n - i; j++)
        {
            printf(" ");
        }
        while (k != 2 * i - 1)
        {
            printf("*");
            k++;
        }
        printf("\n");
    }

    return 0;
}

Give Same Suggestion