Search C Program

Number Pattern - 54

1     1
 2   2 
  3 3  
   4   
  3 3  
 2   2 
1     1

int main()
{
    int i,j,k=1;
    int m[7][7]={0};
    for(i=1;i<=7;i++)
    {
        for(j=1;j<=7;j++)
            if(j==i || 8-i==j) 
                m[i-1][j-1]=k;
            if(i<4) k++;
            else --k;

    }
    for(i=0;i<7;i++)
    {
        for(j=0;j<7;j++)
        {
            if(m[i][j]==0)
                printf(" ");
            else
                printf("%d",m[i][j]);
        }
        printf("\n");
    }
    return 0;
}


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



8 comments:

  1. Can you please explain this code in detail so that we can understand easily... I am also learning c lang. and I do need this pattern but can't understand how do you put that logic. :)

    ReplyDelete

  2. use this code you can easily understood, and this code is less complex as compare to above one.

    #include
    #include

    int main()
    {
    int i,j,k=0,n,l=1,sp=5;
    printf("Enter the number of line:\n");
    scanf("%d",&n);

    for(i=0;i<2*n-1;i++)
    {
    for(j=0;j<k;j++)
    printf(" ");

    printf("%d",l);

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

    }
    }

    ReplyDelete
  3. #include
    #include
    void main()
    {
    int n,i,j;
    printf("enter the side of square");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    if(i==j||n+1-i==j)
    if(i<=n/2||j<=j/2)
    printf("%d",i);
    else
    printf("%d",n+1-i);
    }
    else
    printf(" ");
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  4. #include
    void main()
    {
    int i,j,k=1,l=1;
    i=1;
    while(i<=4)
    {
    j=1;
    while(j<=4)
    {
    if(i==j)
    {
    printf("%d",k++);
    }
    else
    {
    printf(" ");
    }
    j++;
    }
    j=1;
    while(j<=3)
    {
    if(j+i==4)
    {
    printf("%d ",l++);
    }
    else
    {
    printf(" ");
    }
    j++;
    }
    i++;
    printf("\n");
    }
    i=1;
    l=3;
    k=3;
    while(i<=3)
    {
    j=3;
    while(j>=0)
    {
    if(i==j)
    {
    printf("%d",l--);
    }
    else
    {
    printf(" ");
    }
    j--;
    }
    j=1;
    while(j<=3)
    {
    if(j==i)
    {
    printf("%d",k--);
    }
    else
    {
    printf(" ");
    }
    j++;
    }
    i++;
    printf("\n");
    }
    }

    ReplyDelete
  5. General form of code for any value of n------------->>>>>>
    #include
    using namespace std;
    int main()
    {
    int n;
    cin>>n;
    int size=2*n-1;
    int mid=(size-1)/2;
    int x=1;
    int p=0,q=size-1;
    int flag=0;
    for(int i=0;i<size;i++)
    {
    for(int j=0;j<size;j++)
    {
    if(p==j||q==j)
    printf("%d",x);
    else
    cout<<" ";
    }
    if(p==q)
    {
    flag=1;
    }
    if(flag==1)
    {
    p--;
    q++;
    x--;
    }
    else{p++;q--;x++;}
    cout<<endl;
    }
    }

    ReplyDelete
  6. public class type {
    public static void main(String...s)
    {
    for(int i=1;i<=7;i++)
    {
    for(int j=1;j<=7;j++)
    {
    if(j==i|| j==(8-i))
    {
    System.out.print(i);

    }
    else
    {System.out.print(" ");}


    }System.out.println();
    }}}

    ReplyDelete
  7. Can anyone help me with program to generate below pattern using symbols:
    H

    ReplyDelete
  8. #include
    int main()
    {
    int i,j,n,k=1;
    printf("enter the value of rows");
    scanf("%d",&n);
    for(i=0;i<=n/2;i++)
    {
    for(j=0;j
    int main()
    {
    int i,j,n,k=1;
    printf("enter the value of rows");
    scanf("%d",&n);
    for(i=0;i<=n/2;i++)
    {
    for(j=0;j<n;j++)
    {
    if((i==j)||(j==n-i-1))
    {
    printf("%d",k);
    }
    else
    {
    printf(" ");
    }

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

    ReplyDelete