Thursday 23 April 2015

AMCAT PATTERN {1,3*2,4*5*6,10*9*8*7}

The most common pattern is
for n=5
we have output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15


The Solution is:

#include <iostream>

using namespace std;

int main()
{
   int n=5,num;

   num=1;
   int l=1;
   int k=num;
   for(int i=1;i<=n;i++)
   {
     k=num-1;
     
       for(int j=1;j<=num;j++)
       {
       
           if(j%2==0)
           cout<<"*";
           else
           {
               if(i%2==0)
               {
             
               cout<<k+l-num+i;
               l++;
               k=k-2;
               }
             
              else
               cout<<l++;
           }
       }
       num=num+2;//the loop is going on as a prime number 1,3,5,7
       cout<<"\n";
   }
   return 0;
}

32 comments:

  1. Can u pls type the code in c

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. #include

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

      }
      }printf("\n");
      }
      else
      {
      l=((i*i+i)/2);

      for(int j=1;j<=2*i-1;j++)
      {
      if(i%2==0&&j%2==0)
      {
      printf("*");
      }
      else
      {
      printf("%ld",l);
      l--;
      k++;
      }

      }printf("\n");
      }
      }
      }

      Delete
    3. how to print same in reverse way.
      like 11*12*13*14*15
      10*9*8*7
      4*5*6
      3*2
      1

      Delete
    4. why are you cheking the conditions again.
      if(i%2!=0&&j%2==0), no need of i%2!=0 becuase, you have entered the loop after cheking it only, same in else case also.

      Delete
  2. import java.io.*;
    import java.net.*;
    public class Selenium_Projext {
    public static void main(String[] args){

    PrintPat(9);
    }

    public static void PrintPat(int a)
    { int last=0;
    for(int i=1;i<=a;i++)
    { last=last+i;
    int l=i;

    for(int j=last;l>0;l--)
    {

    System.out.print(j--);
    if(l>1)
    System.out.print("*");

    }

    System.out.println();
    }
    }}

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. pattern is not the same as expected

      Delete
  3. Thanks for writing the same pattern in Java..!

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. In C (easy way)..

    #include
    int main()
    {
    int i,j,n,count=0,k=0;
    printf("Enter N");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    count=k;
    for(j=1;j<=i;j++)
    {
    if(i%2==0)
    {
    printf("%d",count+i);
    count=count-1;
    if(j!=i)printf("*");
    k++;
    }
    else
    {
    count=count+1;
    printf("%d",count);
    if(j!=i)printf("*");
    k++;
    }
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  6. import java.io.*;
    import java.net.*;
    public class mindtree {
    public static void main(String[] args)
    {

    PrintPat(5);
    }

    public static void PrintPat(int n)
    {
    int i,j,num=1,k=num,I=1;
    for(i=1;i<=n;i++)
    {
    k=num-1;
    for(j=1;j<=num;j++)
    {
    if(j%2==0)
    System.out.print("*");
    else
    {
    if(i%2==0)
    {
    System.out.print(k+I-num+i);
    I++;
    k=k-2;
    }
    else
    System.out.print(I++);
    }
    }
    num=num+2;
    System.out.println();
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. #include
    int main(){
    int i,j,k=1;
    for(i=1;i<=5;i++){

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

    ReplyDelete
    Replies
    1. Your code is not for the pattern asked, its for different pattern

      Delete
  9. code in c language(easy to understand)

    #include
    void main()
    {
    int i,j,k=1,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    if(i%2!=0)
    {
    for(j=1;j<=i;j++)
    {
    if(j<i)
    printf("%d*",k++);
    else
    printf("%d",k++);
    }
    }
    else
    {
    for(j=1;j<=i;j++)
    {
    if(j<i)
    {
    if(j==1)
    k=k+i-1;
    printf("%d*",k--);
    }
    else
    printf("%d",k--);
    if(j==i)
    k=k+i+1;
    }
    }
    printf("\n");
    }
    }

    ReplyDelete
    Replies
    1. awesome man, this was one of the easiest way :)

      Delete
    2. super! all test casses are passed.

      Delete
  10. #include
    #include
    using namespace std;
    int main()
    {
    int i,j,k=1;
    for(i=1;i<=5;i++)
    {
    if(i%2==0)
    {
    int temp=k-1+i;
    for(j=1;j<=i*2-1;j++)
    {
    if(j%2==0)
    {
    printf("*");
    }
    else
    {
    printf("%d",temp);
    temp--;
    k++;
    }

    }

    }
    else
    {
    for(j=1;j<=2*i-1;j++)
    {
    if(j%2==0)
    {
    printf("*");
    }
    else
    {
    printf("%d",k);
    k++;
    }

    }
    }
    printf("\n");
    }
    }

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Python 3.x code:

    count,k = 0,0
    n = int(input("enter n:"))

    for i in range(1,n+1):
    count = k
    for j in range(1,i+1):
    if i%2 == 0:
    print(count+i,end="")
    count = count - 1
    if j!=i:
    print("*",end="")
    k += 1
    else:
    count = count +1
    print(count,end="")
    if j!=i:
    print("*",end="")
    k += 1
    print("\r")

    It doesn't take proper indentation when i'm posting it.
    if anyone knows how to paste a code with proper indentation in this blog.please suggest me.
    like in stackoverflow we do ctrl+k for code.

    ReplyDelete
  14. count,k = 0,0
    n = int(input())

    for i in range(1,n+1):
    count = k
    for j in range(1,i+1):
    if i%2 == 0:
    print(count+i,end="")
    count = count - 1
    if j!=i:
    print("*",end="")
    k += 1
    else:
    count = count +1
    print(count,end="")
    if j!=i:
    print("*",end="")
    k += 1
    print("\r")





    ReplyDelete
  15. count,k = 0,0
    n = int(input())

    for i in range(1,n+1):
    count = k
    for j in range(1,i+1):
    if i%2 == 0:
    print(count+i,end="")
    count = count - 1
    if j!=i:
    print("*",end="")
    k += 1
    else:
    count = count +1
    print(count,end="")
    if j!=i:
    print("*",end="")
    k += 1
    print("\r")





    ReplyDelete
  16. how to write java pattern program in below output
    1*2*3
    7*8*9
    4*5*6

    ReplyDelete
  17. 1112
    3222
    3334
    how to write java pattern?

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      int i,j,n;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=n+1;j++)
      {
      if(i%2!=0)
      {
      if(j<=n)
      {
      printf("%d",i);
      }
      else
      {
      printf("%d",i+1);
      }
      }
      else
      {
      if(j==1)
      {
      printf("%d",i+1);
      }
      else
      {
      printf("%d",i);
      }
      }
      }
      printf("\n");
      }
      return 0;
      }

      Delete