Search C Program | nimishsoft@gmail.com

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


14 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
  4. i want opposite of this pattern plz help
    i want this :
    111111111
    122222221
    123333321
    123444321
    123454321
    123444321
    123333321
    122222221
    111111111

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i, j, n=5;

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

      Delete
    2. hi can u explain what %d stands for?

      Delete
  5. #include
    int main()
    {
    int i, j, n=5;

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

    ReplyDelete
  6. #include
    #include
    #include
    #include

    int main()
    {

    int n;
    scanf("%d", &n);
    int len=n*2-1;
    for(int i=0;i<len;i++){
    for(int j=0;j<len;j++)
    {
    int min= i<j?i:j;
    min= min<len-i?min:len-i-1;
    min= min<=len-j-1? min:len-j-1;
    printf("%d" " ",n-min);
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  7. #In python
    n=int(input())
    h=(2*n-1)
    r=0
    q=0
    w=0
    v=0
    a=1
    e=0
    for i in range(h):
    if ir and j=h-1-r and j=n):
    print(n-v+w+1,end=" ")
    w=w+1
    else:
    print(n-v+w,end=" ")
    w=w+1
    else:
    print(" ",end=" ")
    print()
    r=r+1
    v=v+1
    q=0
    w=0

    else:
    f=i
    g=i-r+2

    for j in range(h):
    if(i==h-1 or j==0 ):
    print(n,end=" ")
    elif( j=f-a and j=h-1-w-r+2 and j<h):
    print(g+e,end=" ")
    e=e+1
    else:
    print(" ",end=" ")
    print()
    f=f+1
    e=0
    q=0
    w=w-1
    g=g+1
    a=a+2

    ReplyDelete
  8. 555555555
    544444445
    543333345
    543222345
    543212345
    543222345
    543333345
    544444445
    555555555

    #include
    using namespace std;
    int main(){
    int i,j,N,count=0,tao;
    cin>>N;
    i=1;
    while(i<=N){
    j=1;
    count=1;
    while(j<=2*N-1){
    if(count<=i){
    tao=N-j+1;
    cout<<tao;
    } else if (count<=2*N-i){
    cout<<tao;
    } else {
    cout<<tao+j-(2*N-i);
    }
    count++;
    j++;
    }
    cout<<endl;
    i++;
    }
    i=1;
    while(i<N){
    j=1;
    count=1;
    while(j<=2*N-1){
    if(count<=N-i){
    tao=N-j+1;
    cout<<tao;
    } else if (count<=N+i){
    cout<<tao;
    } else {
    cout<<tao+j-(N+i);
    }
    count++;
    j++;
    }
    cout<<endl;
    i++;
    }
    return 0;
    }

    ReplyDelete