Printing reverse number pattern examples using For Loop in C++


A nested for loop is a loop inside another loop. The outer loop is executed first, followed by the inner loop. The inner loop is executed completely for each iteration of the outer loop. Here's the general structure of a nested for loop:

Syntax:
   for( initial ; Condition ; increment / decrement )  // Outer Loop Statements
   {
      for( initial ; Condition ; increment / decrement )  // Inner Loop Statements
      {
          // code block to be executed
      }
   }

In this loop, the outer loop runs first and initializes a counter variable. The outer loop also has a condition that is checked before each iteration. If the condition is true, the inner loop is executed. The inner loop also has a counter variable, a condition, and an update statement. The code block inside the inner loop is executed for each iteration of the inner loop.

This program prints a pattern of numbers in descending order

  • The program starts by initializing three variables: i, j, and a. a is used to store the input value entered by the user.
  • The program then prompts the user to enter a value.
  • In the for loop, the program prints a new line and then prints a sequence of numbers starting from i down to 1. The inner loop runs from j to 1 and prints the value of j on each iteration.
  • Finally, the program returns 0 to indicate successful completion.
#include<iostream>
using namespace std;
int main()
{
    int i,j,a;
    cout<<"\nEnter the value:";
    cin>>a;
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(j=i;j>0;j--)
        {
            cout<<j;
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
1
21
321
4321
54321
654321
7654321
87654321
987654321
10987654321

Source Code Example : 2

#include<iostream>
using namespace std;
int main()
{
    int i,j,a;
    cout<<"\nEnter the value:";
    cin>>a;
    for(i=1;i<=a;i++)
    {
        cout<<"\n";
        for(j=i;j>0;j--)
        {
            cout<<j;
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
1
21
321
4321
54321
654321
7654321
87654321
987654321
10987654321

Source Code Example : 3

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=n-i;j++)
        {
            cout<<" ";
        }
        for(j=n;j>n-i;j--)
        {
            cout<<j;
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
         10
        109
       1098
      10987
     109876
    1098765
   10987654
  109876543
 1098765432
10987654321

Source Code Example : 4

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=0;i<n;i++)
    {
        cout<<"\n";
        for(j=n-i;j<=n;j++)
        {
            cout<<j;
        }
    }
}
To download raw file Click Here

Output

Enter the value:10
10
910
8910
78910
678910
5678910
45678910
345678910
2345678910
12345678910

Source Code Example : 5

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=i;j<=n;j++)
        {
            cout<<j;
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
12345678910
2345678910
345678910
45678910
5678910
678910
78910
8910
910
10

Source Code Example : 6

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";

        for(j=1;j<=i;j++)
        {
            cout<<" ";
        }
        for(j=n;j>=i;j--)
        {
            cout<<j;
        }
    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
10987654321
  1098765432
   109876543
    10987654
     1098765
      109876
       10987
        1098
         109
          10

Source Code Example : 7

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\nEnter the value:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=n-i;j++)
        {
            cout<<"  ";
        }
        for(j=n;j>n-i;j--)
        {
            cout<<" "<<j;
        }
        for(j=n-i+2;j<n+1;j++)
        {
            cout<<" "<<j;
        }
    }
    for(i=1;i<=n;i++)
    {
        cout<<"\n";
        for(j=1;j<=i;j++)
        {
            cout<<"  ";
        }
        for(j=n;j>i;j--)
        {
            cout<<" "<<j;
        }
        for(j=i+2;j<=n;j++)
        {
            cout<<" "<<j;
        }

    }
    return 0;
}
To download raw file Click Here

Output

Enter the value:10
                   10
                 10 9 10
               10 9 8 9 10
             10 9 8 7 8 9 10
           10 9 8 7 6 7 8 9 10
         10 9 8 7 6 5 6 7 8 9 10
       10 9 8 7 6 5 4 5 6 7 8 9 10
     10 9 8 7 6 5 4 3 4 5 6 7 8 9 10
   10 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 10
 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
   10 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 10
     10 9 8 7 6 5 4 3 4 5 6 7 8 9 10
       10 9 8 7 6 5 4 5 6 7 8 9 10
         10 9 8 7 6 5 6 7 8 9 10
           10 9 8 7 6 7 8 9 10
             10 9 8 7 8 9 10
               10 9 8 9 10
                 10 9 10
                   10
                    

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples