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


3 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