Search C Program | nimishsoft@gmail.com

Number Pattern - 52

1
3 2
4 5 6
10 9 8 7
11 12 13 14 15

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


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


9 comments:

  1. Hey the output of your given code does not correspond to the expected output.
    Here is the my code that gives that output

    import java.util.Scanner;

    /* 1
    * 3 2
    * 4 5 6
    * 10 9 8 7
    * 11 12 13 14 15
    */
    public class UltaPulta
    {
    public static void main(String[] args)
    {
    int count = 1;
    Boolean direction = true;
    int reverse = 0;
    System.out.println("Enter number of rows to print :");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for(int i = 1; i < n; i++)
    {
    if(direction == true)
    {
    for(int j = 1; j <= i; j++)
    {
    System.out.print(count+" ");
    count++;
    }
    direction = false;
    }
    else
    {
    reverse = count + i -1;
    for(int j = i; j >=1; j--)
    {
    System.out.print(reverse+" ");
    reverse--;
    count++;
    }
    direction = true;
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  2. Alternate code for the above output ??

    ReplyDelete
    Replies
    1. I hv the altetnate solution and very simple coding

      Delete
    2. yes...i have a solution...it is in java language..you can change the syntax..the logic would be same
      public class d
      {
      public static void main(String[] args)
      {
      int i,j,b=1,r,c;

      for(i=1;i<=5;i++)
      {

      if(i%2!=0)
      {
      for(j=1;j<=i;j++)
      {

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

      c=b-1;
      r=c+i;
      for(j=1;j<=i;j++)
      {

      System.out.print(r+" ");
      r--;

      }
      b=c+i+1;
      }


      System.out.print("\n");
      }
      }
      }

      Delete
  3. No the above logic code is giving correct outputoutput & is smaller

    ReplyDelete
  4. #include
    #include
    #include

    int main(){

    int i,j;

    for(i=1;i<=5;i++){
    if(i%2!=0){

    for(j=(i*i-i+2)/2;j<=(i*i+i)/2;j++){
    printf("%d ",j);

    }
    }
    else{
    for(j=(i*i+i)/2;j>=(i*i-i+2)/2;j--){
    printf("%d ",j);
    }
    }
    printf("\n");
    }
    }

    ReplyDelete
  5. the code given by admin is difficult to understand.Here is the most simplest solution and shorter than all the solutions given.
    int main()
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=i;j++)
    {
    if(i%2==0)
    {
    value=(i*i+1)/2;
    printf("%d",value);
    value--;
    }
    else
    {
    value=((i*i-1)/2)+1;
    printf("%d",value);
    value++;
    }
    }
    printf("\n");
    }
    return 0;

    ReplyDelete