Very Simple C++ programs – Nested loops Demonstration/Examples

Here are some extremely simple programs in C++ for absolute beginner students. I hope these would help them understand how nested loops work in programming. The required pattern of numbers to be printed on screen by a specific program is given before the source code to serve a problem statement.

1)
Required Pattern
1
12
123
1234
12345

2)
Required Pattern
12345
1234
123
12
1

3)

Required Pattern
1
21
321
4321
54321

4)

Required Pattern
54321
54321
54321
54321
54321

Enjoy!

If you can’t understand any step of any of these programs then please let me know via comments. I shall try my best to help you in understanding there execution.

150 thoughts on “Very Simple C++ programs – Nested loops Demonstration/Examples”

  1. plz help me to program to simulate an analog watch (number 1 to 12 to be arranged in circular fashion with all the three pointers for second minutes and hours ) on the screen. Use nested for loops.

    Use (.) dot for seconds pointer

    Use (*) star for minutes pointer

    Use (#) hash for hours pointer.

    1. #include
      #include
      voidmain()
      {
      int r,c;
      clrscr();
      for(r=1;r<=4;r++)
      {
      for(c=1;c<=r;c++)
      cout<<"1";
      }
      cout<<"endl";
      getch();
      }

    1. #include
      #include
      voidmain()
      {
      int r,c;
      clrscr();
      for(r=1;r<=3;r++)
      {
      for(c=1;c<=4;c++)
      cout<<"*";
      }
      cout<<"endl";
      getch();
      }

    1. int main()
      {
      int i;
      for(i=1;i0;j–)
      {
      cout<<"t";
      }
      cout<0;k–)
      {
      for(int j=k;j>0;j–)
      {
      cout<<"t";
      }
      cout<<endl;
      }
      getch();
      return 0;
      }

    1. #include
      using namespace std;
      int main()
      {
      for(int i=0;i<=2;i++){
      for(int j=0;j<=2;j++)
      if(i==2||i==0&&j==1||i==0&&j==2)
      {
      cout<<"*";}
      else
      cout<<"";
      cout<<"\n";
      }
      }

    1. #include
      using namespace std;
      int main()
      {
      int x=5,y=1,z=1,n,r=5;
      for(int i=1;i<=6;i++)
      {
      for(int j=1;j<=x;j++)
      {
      cout<<"#";
      }
      x–;
      for(int k=1;k<=y;k++)
      {

      cout<<"*";
      }

      for(int l=1;l<z;l++)
      {
      cout<<"#";
      //z=1;
      }
      z+=2;
      for(int m=1;m<=n;m++)
      {

      cout<<"*";
      n=1;
      }
      n+=1;
      for(int a=1;a<=r;a++)
      {
      cout<<"#";
      }
      r-=1;
      cout<<endl;
      }

      }

      1. #####**#####
        ####*##*####
        ###*####*###
        ##*######*##
        #*########*#
        *##########*
        this is what i get when this is run which is fine, but it does not start with #####*##### and it would only run by changing x- to x– or x-=1 as i am a complete beginner i would be happy if you could explain the for loops so i can understand what is going on

  2. 10 10 10 10 10 10 10 10 10 10
    9 9 9 9 9 9 9 9 9
    8 8 8 8 8 8 8 8
    7 7 7 7 7 7 7
    6 6 6 6 6 6
    5 5 5 5 5
    4 4 4 4
    3 3 3
    2 2
    1
    Plz write programm for this plz plz

    1. #include
      using namespace std;
      int main ()
      {
      int num=9;
      for (int i=1;i<=10;i++)
      {
      cout << "10" << " ";
      }
      cout <=1;j–)
      {
      for (int k=1;k<=j;k++)
      {
      cout << num << " ";
      }
      cout << endl;
      num–;
      }
      return 0;
      }

    2. #include
      using namespace std;
      int main()
      {
      for(int i=10; i>=1; i–)
      {
      for(int j=0; j<i; j++)
      cout<<i;
      cout<<endl;
      }
      }

    1. #include
      using namespace std;
      int main()
      {
      for(int i=1; i<=5; i++)
      {
      for(int j=0; j<i; j++)
      cout<<i;
      cout<<endl;

      }
      }

  3. please tell me hoe to do it?
    1******

           12*****

           123****

           1234***

           12345**

           123456*

           1234567

    1. #include
      using namespace std;
      int main()
      {
      int n=5;
      int m=1;
      for (int i=1;i<=7;i++)
      {
      for (int j=1;j<=m;j++)
      {

      cout <=i;k–)
      cout << "*";
      cout << endl;
      m++;
      }
      }

  4.       1******

           12*****

           123****

           1234***

           12345**

           123456*

           1234567

    1. #include
      #include
      using namespace std;

      int main()
      {
      int c,r;
      for(c=3; c<=3; c++)
      {
      cout<<c<<endl;
      }
      for (r=1; r<=5; r++)
      {
      cout<<r;
      }
      for(c=3; c<=3; c++)
      {
      cout<<"\n"; //for new line
      cout<< c;
      cout<< "\n"; //for new line
      cout<<c;

      }
      getch();
      return 0;

    1. #include
      #include
      #include
      void main(void)
      {
      clrscr();
      for( int i =1 ; i <=5 ; i ++)
      {
      cout<<" \n "; // for a new line
      for( int j =1 ; j <=5 ; j ++ )
      {
      cout<<" * ";
      }
      cout<<" \n ";
      }
      getche();
      }

    2. #include
      using namespace std;
      main()
      {
      for(int i=1;i<=4;i++)
      {
      for(int j=1;j<=i;j++)
      {
      cout<4;k–)

      cout<<"*";
      cout<<endl;
      }
      }
      }
      }

    3. int main()
      {
      int i,p;
      for (i=1; i<=5; i++)
      {
      for(p=1; p<=5; p++)
      cout<<"*";
      cout<<"\n"; //new line;
      }
      return 0;
      }

    1. #include
      #include
      #include
      using namespace std;
      int main()
      {
      int a,b=0,c,d,n=1;
      for(int i=1;i<5;i++)
      {

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

  5. Write a c++ programs that use two nested loops to generate the following input
    1 2 3 4=10
    5 6 7 8=26
    9 10 11 12=42
    13 14 15 16=58
    plz tell me its urgent

    1. just use printf or cout. Enjoy it !!!!!
      Logic will remain same.

      Write: printing purpose and
      WriteLine: printing+new line.

      for (int i = 1; i <= 13; i+=4)
      {
      int sum=0;

      for (int j = i; j <=i+3; j++)
      {
      sum+=j;
      if (j == i + 3)
      {
      Console.Write(j.ToString() + "=" + sum.ToString());

      }
      else
      {
      Console.Write(j.ToString() + " ");
      }
      }

      Console.WriteLine();
      }

    2. #include
      #include
      #include
      using namespace std;
      int main()
      {
      int a,b=0,c,d,n=1,sum=0;
      for(int i=1;i<5;i++)
      {

      for(int j=1;j<=4;j++)
      {
      sum=sum+n;
      cout<<n;
      n++;
      }
      cout<<"="<<sum;
      cout<<endl;
      sum=0;
      }
      return 0;
      }

    1. This structure is known as Pascal Triangle and it has a specific coding. Here’s the coding :
      #include
      #include
      long calc( int );
      int main()
      {
      int i,j,row,pas;
      printf(“Enter no. of rows in pascal triangle : “);
      scanf(“%d”, &row);
      for(i=0; i<row; i++)
      {
      for(j=0; j<=(row-i-1); j++)
      printf(" ");
      for(j=0; j<=i; j++)
      {
      pas=calc(i)/(calc(j)*calc(i-j));
      printf("%ld ",pas); //take single space
      }
      printf("\n");
      }
      getch();
      return 0;
      }

      long calc( int num)
      {
      int x;
      long res=1;
      for(x=1; x<=num; x++)
      res=res*x;
      return (res);
      }

      In main, formula of pas is in the form of factorial formula.

  6. Write a program using nested loops which generate the following output:
    (1,1)(1,2)(1,3)
    (2,1)(2,2)(2,3)…
    The value of the first number has to be taken from the user.

    How i can solve this?

    1. just use printf or cout. Enjoy it !!!!!
      Logic will remain same.

      Write: printing purpose and
      WriteLine: printing+new line.

      int ii,jj,kk;
      for (ii = 1; ii <= 5; ii++)
      {
      for (jj = ii; jj <= 5; jj++)
      {
      Console.Write(jj.ToString());
      }

      for (kk = 1; kk < ii; kk++)
      {
      Console.Write(kk.ToString());
      }

      Console.WriteLine();
      }

    1. just use printf or cout. Enjoy it !!!!!
      Logic will remain same.

      Write: printing purpose and
      WriteLine: printing+new line.

      for (int i=1; i = 1; j–)
      {
      Console.Write(j);
      }
      Console.WriteLine();
      }

    1. {
      //print the following program use cout statment ?
      /*cout<<"*******"<<endl;
      cout<<"* *"<<endl;
      cout<<"* *"<<endl;
      cout<<"* *"<<endl;
      cout<<"*******"<<endl;*/
      // following program use nested loop
      int i;
      for(i=1;i<=7;i++)
      {
      cout<<"*";
      }

      cout<<endl;
      {
      for(int j=1;j<=3; j++)
      cout<<"* *"<<endl;
      }

      for(i=1;i<=7;i++)
      cout<<"*";

      return 0;
      }

    2. #include
      using namespace std;

      int main ()
      {
      int i, j;

      for(i=1; i<=2; i++) {
      cout<<"********\n";
      for(j=1; j <=3; j++){
      cout<<"**\n";
      } cout<<"********\n";
      break;
      }
      return 0;
      }

    1. just use printf or cout. Enjoy it !!!!!
      Logic will remain same.

      Write: printing purpose and
      WriteLine: printing+new line.

      int u = 0;
      for (int i = 1; i < 5; i++)
      {
      for (int j = 1; j <= u+i; j++)
      {
      Console.Write(i);

      }
      u = u + 1;
      Console.WriteLine();
      }

    2. #include
      using namespace std;
      void main()
      {
      int row,st,space,row1,n=1;

      for(row=1;row<=5;row++){

      for( st=1;st<=row;st++){

      cout<<n;

      }
      cout<<endl; n++;
      //dec++;
      //inc++;

      }
      cin.get();

      }

    3. #include
      #include
      main()
      {
      int a,b;
      for(a=1;a<=5;a++)
      {
      for(b=1;b<=a;b++)
      {
      printf("%d",a);
      }
      printf("\n");
      }
      }

  7. Write a c++ programs that use two nested loops to generate the following input
    1 2 3 4=10
    5 6 7 8=26
    9 10 11 12=42
    13 14 15 16=58

    1. salam…here is a pattern to solve ur qs4n

      #include “stdafx.h”
      #include
      using namespace std;

      int main()
      {
      int number, reverse = 0;
      cout<> number; // Taking Input Number in variable number

      for( ; number!= 0 ; )
      {
      reverse = reverse * 10;
      reverse = reverse + number%10;
      number = number/10;
      }
      cout<<"New Reversed Number is: "<<reverse<<endl;
      system("pause");
      return 0;

      }

    2. main()
      {
      int a,b,c,d;
      cin>>a; //1234
      b=a/1000;
      a=a%1000;
      c=a/100;
      a=a%100;
      d=a/10;
      a=a%10;
      cout<<a<<d<<c<<b;
      }
      output:
      4321

    1. #include
      #include
      using namespace std;
      int main()

      {
      for(int i =0;i<4;i++)
      {

      for(int a=0;a<4;a++)
      {
      cout<<"1";

      }
      cout<<"\n\n";
      }

      getch();
      }

      1. I used this code to implement this shape

        * * * * * * *
        * * * * * * *
        * * * * * * *
        * * * * * * *
        * * * * * * *
        * * * * * * *
        * * * * * * *

        I must now produce this pattern

        * * * . * * *
        * * * . * * *
        * * * . * * *
        . . . . . . .
        * * * . * * *
        * * * . * * *
        * * * . * * *

        please help?

        1. I did it!

          for (int row = 0; row < 7; row++) // Define amount of rows
          {
          for (int col = 0; col < 7; col++) // Define number of columns within each row
          {
          if (row == 3 || col == 3)
          {
          cout << ". ";
          }
          else
          {
          cout << "* "; // Print a "*" each time the
          }
          }
          cout << :endl;
          }

    1. */Please try this
      #include “stdafx.h”
      #include
      using namespace std;
      void main()
      {
      int i,j,k;
      for(i=0;i<=6;i++)
      {
      for(j=1;j<=i;j++)
      cout<<j;
      cout<<endl;
      for(k=1;k<=6+1-i;k++)
      cout<<"*";
      }
      }

    1. #include
      using namespace std;
      int main()
      {
      for(int i=0;i<=4;i++){
      for(int j=0;j<=4;j++)
      if( i==0||i==4||i==0&&j==2||j==3||i==4||j==1)
      {
      cout<<"*";}
      else
      cout<<" ";
      cout<<"\n";
      }
      }

  8. #include
    int main ()
    {
    int a;
    int b;
    for(a=0;a=0;b–) {
    printf(” “);
    }
    for(b=0;b=a;b++) {
    printf(“%d”,a);
    }
    }
    printf(“\n”);
    }

  9. Can anyone do this?
    Using For Loop, While Loop and DoWhile Loop, create a program that will ask the user to input any number from 1-99. The input of the user will serve as the starting number of the output until it reach 100.

    Sample Output:

    Enter number from 1-99: 40

    40 41 42 43 44 45 46 47 48 49 50 ……..98 99 100

    Would you like to continue? (y/n)

    Note: Any input other than 1-99 should be “invalid input” and should ask again to enter number from 1-99. Would you like to continue (y/n)? question should still be posted every after the display of the output numbers. If user input y or Y, display “Welcome back” and go back to main, if n or N display “Thank you” and exit program. Any other input, display “Invalid input” and go back to the question would you like to continue? (y/n)\

    1. #include
      using namespace std;
      int main(){
      int x=5;
      int i,j;
      for(i=1;i<=5;i++)
      {
      for(j=0;j<x;j++)

      cout<<"B";

      cout<<endl;
      x–;
      }

      return 0;

      }

  10. Can anyone tell me how to do this example
    Design a program that is capable of calculating total of five subjects marks, each of 100 marks. The program will run as long as the user wishes but will work once at least without asking user’s permission.
     Use Nested loop to design the program.
     The inner loop will calculate total of 5 subject marks. Do not use 5 variables for 5 subjects.
     Use outer loop to find if user wishes to continue.
     Validate that user enters marks of each subject between 1 to 100

  11. int n=5
    int m=n
    what does it mean:?
    and i and j is not defined in int so how it can be used in loops:???? plzz tell me .,.,., em a beginner

    1. Sorry for late reply Talha.
      int N = 5;
      defines an integer variable named “N” and sets it equal to 5.
      int M = N;
      defines another integer variable named “M” and sets it equal to the value of the variable N. As we have just assigned a value of 5 to N, it is equivalent to the following statement:
      int M = 5;
      But a slight benefit in doing it the way I have done is that, if on a later time, you decide that both M and N should be initialized at a value of 10 instead of 5, then you will have to make this change at one place in your source code. If you define both variables independently then you will have to make that change at two places. This looks like a very small benefit in this simple example. But if you make it your habit to do things in such centralized ways, then you will see that it will save you from a lot of trouble when you move on to reasonably more complex and longer programs.

      Now lets look at your second question. Why do you think that i and j are not defined as int variables? They are clearly defined in the first line of their respective for loops. In the parenthesis that follow the “for” keyword, you are allowed to put any three C++ statements separated by semicolons. There is no restriction from compiler on what kind of operations could be performed in those statements. Therefore, in these examples, I am defining variables in first of those three statements.

      Hope it helps. Thanks for visiting and best of luck for your newly started programming journey 🙂

    1. #include
      using namespace std;
      int main()
      {
      int i=5;
      int j;
      while(i>0)
      {
      j=i;
      while(j>0)
      {
      cout<<"*";
      j=j-1;
      }
      cout<<endl;
      i=i-1;
      }
      return 0;

      }

    2. #include
      #include
      void main()
      {
      clrscr();
      int i,j;
      for(i=4;i>=1;i–)
      {
      for(j=1;j<=i;j++)
      cout<<"*";
      cout<<endl;
      }
      getch();
      }

    1. First try it yourself. Post what you have come up with here. Then I will correct any mistakes or errors that you might have in your code. In this way, you will learn much more than simply copying the code 🙂

    1. well … make the outer loop iterate for 6 times, and the inner loop to iterate for 2 times in first iteration of the outer loop and progressively reach 7 iterations by the last iteration of the outer loop. Place a check (if statement) in the body of inner loop that prints an underscore (_) if it is the last iteration of the inner loop and a pipe (|) otherwise.

      I hope these hints will be enough for you to write your required program. Try your best and If you are still confused about some specific step then paste the code that you are able to come up with here and I shall try to correct any of your mistakes 🙂

      And, thanks for visiting 🙂

    1. #include
      #include
      void main()
      {
      int a=5;
      int b=a;
      for(int c=0;c<a;c++)
      {
      for(int d=1;d<=b;d++)
      {
      cout<<"*";
      }
      cout<<endl;
      b–;
      }
      }

    2. #include
      #include
      void main()
      {
      int a,b;
      cout<<"enter the value of a:="<<endl;
      for(a=1;a<=5;a++)
      {
      for(b=1;b<=a;b++)
      {
      cout<<"*";
      }
      cout<<endl;
      }
      getch()
      }

    3. #include
      #include
      void main()
      {
      clrscr();
      int i,j,n;
      cout<<"enter any number to find its pattern"<>n;
      for(i=1;i<=n;i++)
      {
      for(j=0;j<=i;j++)
      {
      cout<<"*";
      }

    1. I can’t detect some regular pattern in these lines. can you please tell in some more detail what could be some other legitimate outputs. Then I may be able to help you better.
      if it has some white spaces on the left side of lines which are not showing here, then please repost it by enclosing it within
      [codesyntax lang=”php”]

      [/codesyntax]
      tags.
      Thanks

  12. this is the continuation:
    for (int l=(N-1)/2; l>=1; l–) //l=line number after mid to end in descending order after mid.
    { //so if line mid=3, the line after it is line 2 and then line 1.
    for (int s=1; s<=space_2; s++) //s=spaces on each line l.
    cout<<" ";
    for (int a=(space_2)+1; a<=(N-space_2); a++) //a=place os asterisks on each line l.
    cout<<"*";
    cout<=1; l–) //l=line number after mid to end in descending order after mid.
    { //so if line mid=3, the line after it is line 2 and then line 1.
    for (int s=1; s<=space_2; s++) //s=spaces on each line l.
    cout<<" ";
    for (int a=(space_2)+1; a<=(N-space_2); a++) //a=place os asterisks on each line l.
    cout<<"*";
    cout<<endl;
    space_2+=1; //updating space_2.
    }
    But the shape above is ot how i meant it. i meant them to be aligned in the middle.
    thank you.

  13. I meant something like this
    *
    ***
    *****
    ***
    *
    el hamdulellah using this code, it was printed but I don’t know if there’s a simpler way because it seems complicated:
    int mid=(N+1)/2; //the line in the middle of the rhomboid.
    int space=(N-1)/2; //the number of spaces before the ‘*’. Is to be used for line 1 of the rhomboid to line mid.
    int space_2=1; //to be used for spaces before’*’ for lines after line mid untill the last line.

    for (int l=1; l<=mid; l++) //l=line number from 1 to mid.
    {
    for (int s=1; s<=space; s++) //s=spaces on each line l.
    cout<<" ";
    for (int a=space+1; a<=(N-space); a++) //a=place of asterisk on each line 1.
    cout<<"*";
    cout<<endl;
    space-=1; //updating space.
    }

  14. Yes .. I shall try my best to help you. Can u please tell me for a given value of N, say N=5, what should be the output of this program?? I mean the correct output as asked in the problem statement. Currently for N=5 it prints 5 lines containing 5 asterisks each like this:
    [codesyntax lang=”cpp”]
    *****
    *****
    *****
    *****
    *****
    [/codesyntax]
    do you want to print:
    [codesyntax lang=”cpp”]
    *
    ***
    *****
    ***
    *
    [/codesyntax]
    or something else for N=5??
    and … Thanks for visiting!

  15. I am trying to do a program that prints asterisks in the shape of a diamond with N lines and this where i got so far. Is it possible to teach me how to continue without telling me the answer. Thank you.

    for (int k=N; k>0; k–)
    {
    for (int i=1; i<=N; i++)
    cout<<"*";

    cout<< endl;
    }

    1. Have you attempted it yourself?? what specific problem do you have??
      Pease first show me what you have come up with and then I shall try my best to help you correct your mistakes 🙂
      However if you don’t know how to generate successive alphabets in a program then I can give you a hint in this regard.
      Alphabets ‘A’ to ‘Z’ correspond to ASCII values 65 to 90 respectively. It means that if you assign a number in this range to a variable of type ‘char’ and then output this variable to screen then the alphabet corresponding to that number will be printed.
      Look at this simple example program. It prints all capital alphabets from ‘A’ to ‘Z’ on the screen.

      char ch;
      for ( int i=65; i<=90; i++ ) { ch = i; cout << ch << endl; } Now that you have the fundamental skill to get the required output, please try to design the required nested loops yourself and in case of any problem show me your code and I shall try to help you 🙂 Thanks for visiting though!!

  16. what will be the program for followiing output by using nested loop in c++.

    5 1
    5 2
    5 3
    5 4
    4 1
    4 2
    4 3
    3 1
    3 2
    2 1

    its my assignment urgent reply..

    1. sorry for the late reply 🙂
      plz first attempt it yourself. if you still can’t find a solution or get stuck in an error that you can’t resolve then let me know by showing your code and i shall try to help you ..

      Thanks!!

  17. oh. .thank’s
    i thought it could be a error!
    I’m a second year, com.sci.
    I hope , i can learn m0re about c++,
    thank u very much,,
    🙂

    1. Hi nathaniel,
      can u see the statements like int i=0, int j=0 as the first statements in the corresponding for loops?? They are actually declarations of these variables. the first of three statements in a for loop’s header is executed once at the beginning of the loop. so it causes the variable to be declared, defined and initialized properly before it is used so it doesn’t cause any error in the execution of the program. Hope you got it. if you still have any confusion then please follow back!!
      Thanks for the comment.

      1. oh. .thank’s
        i thought it could cAuse an error!
        Very sorry, ,
        I’m a second year, com.sci. StudEnt,
        I hope , i can learn m0re about c++,,
        thank u very much sir,,,
        🙂

      2. I want pakistan flag code but i dont have a accurate code please help me to create that code.
        Mine code is this…
        #include
        using namespace std;
        int main()
        {
        for(int i=0;i<=15;i++)
        {
        for(int j=1;j<=40;j++)
        {
        system("color 02");
        if(/*borders*/i==1||j==1||i==15||j==40||
        /*Moon*/i==5&&j==29||i==5&&j==28||i==6&&j==27||i==7&&j==26||i==8&&j==26||i==9&&j==27||i==10&&j==28||i==10&&j==29||
        /*star*/i==6&&j==31||i==7&&j==31||i==8&&j==31||i==7&&j==30||i==7&&j==32
        )
        {
        cout<=1&&j<=11)
        {
        cout<<"/";
        }
        else{
        cout<<" ";
        }
        }
        cout<<endl;
        }
        for(int a=1;a<=9;a++)
        {
        cout<<"##"<<endl;
        }
        system("pause");
        return 0;
        }

    1. oh yes … and thank you very much for mentioning. i have just corrected it. Actually before writing this post, I was experimenting with my these programs to check whether they work as expected for other values of N, too. Probably i copied the source code in such a situation.
      Anyhow, I am sorry for the mistake and thank you for indicating it!!!!!!!

        1. #include….
          main()
          {
          clrscr();
          int i,j;
          for(i=1;i<=4;i++)
          {
          for(j=i;j<=4+i; i++)
          {
          cout<<j<<" ";
          }
          cout<<"\n";
          }
          getch()
          }

        1. how can i write this program ?? Print this value by using nested loop in C++
          2471116
          3581217
          4691318
          57101419
          68111520

    2. kindly tell me how to print bellow diagram by using nested loop
      * * * * *
      * *
      * *
      * * * * *
      m very thankful to you

Leave a Reply