Search C Program

Number Pattern - 46

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5
int main()
{
    int i, j, n=5;
    
    for(i=n; i>1; i--)
    {
        for(j=n;j>=1;j--)
        {
            if(j>i) printf("%d ", j);
            else printf("%d ", i);
        }
        for(j=2;j<=n;j++)
        {
            if(j>i) printf("%d ", j);
            else printf("%d ", i);
        }
        printf("\n");
    }    
    for(i=1; i<=n; i++)
    {
        for(j=n;j>=1;j--)
        {
            if(j>i) printf("%d ", j);
            else printf("%d ", i);
        }
        for(j=2;j<=n;j++)
        {
            if(j>i) printf("%d ", j);
            else printf("%d ", i);
        }
        printf("\n");
    }
    
    return 0;
}


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


4 comments:

  1. I still don't get it, can you explain me how the code work?

    ReplyDelete
  2. #include

    int main()
    {
    int r,i,j,a;

    printf("Enter the no. of rows : ");
    scanf("%d",&r);

    for(i=1;i<=r;i++)
    {
    for(j=1,a=r;j<=(2*r)-1;j++)
    {
    if(j>=i && j<=(2*r)-i)
    {
    printf("%d ",a);
    }
    else if(j=1;i--)
    {
    for(j=1,a=r;j<=(2*r)-1;j++)
    {
    if(j>=i && j<=(2*r)-i)
    {
    printf("%d ",a);
    }
    else if(j<i)
    {
    printf("%d ",a--);
    }
    else
    {
    printf("%d ",++a);
    }
    }
    printf("\n");
    }

    return 0;

    }

    ReplyDelete
  3. More Optimized version:

    void main()
    {
    int n=5;
    int nr=n*2-1;
    int nc=n*2-1;
    int i,j,k,ti,tj;

    clrscr();
    for(i=0;i(nr-1))
    {
    ti=nr-1-i;
    tj=nc-1-j;
    }
    else
    {
    ti=i;
    tj=j;
    }
    k=getMin(ti,tj);
    if(k<n)
    cout<<n-k;
    else
    cout<<n-(n-k);
    }
    }

    getch();
    }

    int getMin(int x,int y)
    {
    if(y<x)
    return y;
    else
    return x;
    }

    ReplyDelete