Print pattern of natural numbers - C++ Program

1. Write a C++ program to print the following pattern of natural numbers:

numbers pattern 1

Answer:

#include<iostream>
using namespace std;
int main()
{
        int i, j, n=1, cnt;
        cout<<"\n Enter Number of Rows : ";
        cin>>cnt;
        cout<<"\n";
        for(i=0; i<cnt; i++)
        {
                for(j=0; j<=i; j++)
                {
                        cout<<n<<" ";
                        n++;
                }
                cout<<"\n";
        }
        return 0;
}


Output:

output numbers pattern 1

2. Write a C++ program to print the following pattern:

numbers pattern 2

Answer:

#include<iostream>
using namespace std;
int main()
{
        int i, j, num=1, cnt;
        cout<<"\n Enter Number of Rows : ";
        cin>>cnt;
        cout<<"\n";
        for(i=0; i<cnt; i++)
        {
                num=1;
                for(j=0; j<=i; j++)
                {
                        cout<<num<<" ";
                        num++;
                }
                cout<<"\n";
        }
        return 0;
}


Output:

output numbers pattern 2

3. Write a C++ program to print the following pattern:

numbers pattern 3

Answer:

#include<iostream>
using namespace std;
int main()
{
        int i, j, num=1, cnt;
        cout<<"\n Enter Number of Rows : ";
        cin>>cnt;
        cout<<"\n";
        for(i=0; i<cnt; i++)
        {
                for(j=cnt; j>i; j--)
                {
                        cout<<num<<" ";
                        num++;
                }
                cout<<"\n";
                num=1;
        }
        return 0;
}


Output:

output numbers pattern 3

4. Write a C++ program to print the following pattern:

numbers pattern 4

Answer:

#include<iostream>
using namespace std;
int main()
{
        int i, j, k, num=1, decr=8, count=0, temp, rows;
        cout<<"\n Enter Number of Rows : ";
        cin>>rows;
        for(i=0; i<rows; i++)
        {
                for(k=0; k<decr; k++) // This loop prints the spaces
                {
                        cout<<" ";
                }
                for(j=0; j<i; j++)  // This loop gives the required value to variable num
                {
                        count++;
                }
                num = count;
                temp = num;
                for(j=0; j<i; j++)  // This loop prints the numbers
                {
                        cout<<num--<<" ";
                }
                cout<<"\n";
                num = temp;
                decr=decr-2;
        }
        return 0;
}


Output:

output numbers pattern 4