Search C Program | nimishsoft@gmail.com

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



21 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. int main()
      {
      for(int i=0; i<5; i++){
      for(int k=5; k>i;k--){
      printf(" ");
      }
      for(int j=0; j<=i; j++){
      printf("%d" , j+1);
      }
      for(int p=i; p>0; p--){
      printf("%d", p);
      }
      printf("\n");
      }

      return 0;
      }

      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
    Replies
    1. import java.util.Scanner;

      public class Pattern10
      {
      public static void main(String args[])
      {
      Scanner scan = new Scanner(System.in);
      int row=0;
      System.out.println("Enter the number of Rows");
      row = scan.nextInt();

      for(int i=1;i<=row;i++)
      {
      for(int j=row;j>i;j--)
      {
      System.out.print(" ");
      }
      for(int j=1;j<=i;j++)
      {
      System.out.print(j);
      }
      for(int j=i-1;j>=1;j--)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      scan.close();
      }
      }

      Delete
  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
  10. what will be program for the following output:
    1 2 3 4 5 4 3 2 1
    1 2 3 4 3 2 1
    1 2 3 2 1
    1 2 1
    1

    ReplyDelete
    Replies
    1. public class Pattern
      {
      public static void main(String args[])
      {
      for(int i=5;i>=1;i--)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(j+" ");
      }
      int k = i-1;
      for(int j=k;j>=1;j--)
      {
      System.out.print(j+" ");
      }
      System.out.println();
      }
      }
      }

      Delete
  11. please help me out with this pattern 5
    545
    54345
    5432345

    ReplyDelete
  12. Please help me in
    11
    11 10 11
    11 10 9 10 11
    11 10 9 8 9 10 11

    ReplyDelete