Search C Program

Number Pattern - 56

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

int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
      for(j=5;j>i;j--)
        printf("  ");
      for(j=1;j<=i;j++)
          printf("%d ",j);
      for(j=j-2;j>=1;j--)
          printf("%d ",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



12 comments:

  1. i think here in last loop j should be equal to j-1 not j-2.please reply asap.

    ReplyDelete
    Replies
    1. No buddy, the logic is all right.... it works as fine... try it!!!

      Delete
  2. Can you make this program with instead of ?

    ReplyDelete
  3. The last loop should be initialized with j=i-1.

    ReplyDelete
  4. you can also use $i in place $j and Logic is $j=$i-1: For E.G:=$i;$j--)
    {
    echo "#";
    }
    for($j=1;$j<=$i;$j++)
    {
    echo "$j";
    }
    for($j=$i-1;$j>=1;$j--)
    {
    echo "$j";
    }
    echo "
    ";
    }
    ?>

    ReplyDelete
  5. Generalise 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=0;
    int p=mid,q=mid;
    int flag=0;
    for(int i=0;i=p&&j<=q)
    {
    if(j<=mid)
    {
    printf("%d",++x);
    }
    else
    {
    printf("%d",--x);
    }
    }
    else
    cout<<" ";
    }
    x=0;
    p--;
    q++;
    cout<<endl;
    }
    }

    ReplyDelete
  6. #include
    int main()
    {
    int i, j;
    for(i=1;i<=5;i++)
    {
    for(j=5;j>i;j--)
    printf(" ");
    for(j=1;j<=i;j++)
    printf("%d ",j);
    for(j=i-1;j>=1;j--)
    printf("%d ",j);
    printf("\n");
    }
    return 0;
    }

    This small change will be easy to understand


    It have a doubt what value will be initially taken for j in 3rd loop
    J=j-2
    Please do clarify

    ReplyDelete
    Replies
    1. its the same thing...if u write j=j-2 it takes the last value with which the second loop was ended...that is if the second loop had j=3 as the last value then in the third loop it will start with j=3-2 which is 1

      Delete
  7. sir this logic is not working in java
    plzzz give me a solution

    ReplyDelete
  8. Try something in different way
    #include
    int main(void){

    int i , j , k , l , row ,set ;

    printf("Enter the rows = ");
    scanf("%d",&row);

    for( i = 0 ; i < row ; i ++ ){

    for( j = 0 ; j < ( row - i - 1 ); j ++){

    printf(" ");
    }

    set = 1 ;
    for( k = 0 ; k <= i ; k ++ ){

    printf("%d",set++);

    }

    set = set - 2 ;
    for(l = 1 ; l <= i ; l++){

    if(set != 0)
    printf("%d",set --);

    }

    printf("\n");
    }

    return 1;
    }

    ReplyDelete
  9. Hey guys, I was wondering if you guys know that you could do this pattern by multiplying two numbers and change those two numbers and again multiply. It's a math trick..

    ReplyDelete