Pattern Printing N – Sequence 001

               Pattern Printing N – Sequence 001


The program must accept an integer N and print the pattern as shown in the Example Input/Output.
Input Format:
The first line contains N.
Output Format:
N lines containing the pattern as shown in the Example Input/Output.
Boundary Conditions:
2 <=  N <= 100
Example Input/Output 1:
Input:
4
Output:
1 2 3 4
9 10 11 12
13 14 15 16
5 6 7 8
Example Input/Output 2:
Input:
7
Output:
1 2 3 4 5 6 7
15 16 17 18 19 20 21
29 30 31 32 33 34 35
43 44 45 46 47 48 49
36 37 38 39 40 41 42
22 23 24 25 26 27 28
8 9 10 11 12 13 14

Code:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,i,j,x=1;
cin>>n;
int m;
int arr[n][n];
for(i=0;i<n/2;i++)
    {
     for(j=0;j<n;j++)
        {
            arr[i][j]=x++;
        }
        x+=n;
    }
    x=1;
    if(n%2==1)
        m=n/2+1;
    else
        m=n/2;
for(i=1;i<=n;i++)
{
    if(i>m)
    {
        x=arr[n-i][n-1];
        x++;
    }
    for(j=0;j<n;j++)
    {
        cout<<x++<<” “;
    }
    x+=n;
cout<<“n”;
   
}
}

Please do comment If u have any Queries!

One Comment

Add a Comment

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