Search C Program

Loading...

Number Pattern - 58

1  2  3  4  5  
16          6  
15          7  
14          8  
13 12 11 10 9  
int main()
{
  int i,j,k=6,l=13,m=16;
  for(i=1;i<=5;i++)
  {
    for(j=1;j<=5;j++)
    {
      if(i==1)
        printf("%-3d",j);
      else if(j==5)
        printf("%-3d",k++);
      else if(i==5)
        printf("%-3d",l--);
      else if(j==1)
        printf("%-3d",m--);
      else
        printf("   ");
    }
    printf("\n");
  }
  return 0;
}


Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C



5 comments:

  1. Can someone help in writing the above code using arrays ?

    ReplyDelete
  2. // trying the thoughtworks lab pattern
    #include
    #include
    using namespace std;

    int main()
    {


    cout << "Hello world! enter the number for the patter " << endl;

    int n;
    cin>> n;

    int arr[n][n];
    int a=1;

    int b=(2*n)+(2*(n-2));
    int c=(n);

    int d=((2*n)+(n-2));


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

    for(int i =0; i <n; i++)
    {
    for(int j =0; j <n; j++)
    {
    arr[i][j]=0;


    }}
    //************
    for(int i =0; i <n; i++)
    {
    for(int j =0; j <n; j++)
    {

    if(i==0)

    {
    arr[i][j]=a++;
    }

    if(j==0&&i!=0)
    {
    arr[i][j]=b;
    b--;

    }

    if(j==(n-1))
    {

    arr[i][j]=c;
    c++;
    }

    if(i==(n-1))
    {
    arr[i][j]=d;
    d--;
    }
    else{arr[i][j]==0;}

    }


    }

    int k=0;
    for(int i =0; i <n; i++)
    {
    for(int j =0; j <n; j++)
    {


    cout<<setw(5)<<arr[i][j];


    if(j==(n-1))
    {
    cout<<endl;

    }


    }
    }
    return 0;

    }

    ReplyDelete
    Replies
    1. Please write a code that could be understandable..

      Delete
  3. The code is in java:
    but will work in c++ as well just replace System.out.print("") by print()
    int a[][] = new int[m][n];
    int k=0;
    for(int i=0; i=0 ; i--)
    a[n-1][i] = ++k;
    for(int i=m-2 ; i>0 ; i--)
    a[i][0] = ++k;
    for(int i=0; i<m; i++){
    for(int j=0; j<n ;j++)
    System.out.print(a[i][j] + " ");
    System.out.println("");
    }

    ReplyDelete
  4. Simple code:

    int main()
    {
    int i,j,k,l,p,n;
    n=8;
    p=n+1;
    k=n+n+n-2;
    l=k+n-2;

    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    if(i==1)
    printf("%-3d",j);
    else if(j==n)
    printf("%-3d",p++);
    else if(i==n)
    printf("%-3d",k--);
    else if(j==1)
    printf("%-3d",l--);
    else
    printf(" ");
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete