Thursday, 23 April 2015

AMCAT Programming pattern {1112,3222,3334}

To print the pattern like
 for n=3
the program should print
1 1 1 2
3 2 2 2
3 3 3 4


The solution in c++:


#include <iostream>

using namespace std;

int main()
{
   int n=3,c=n-1;
   for(int i=1;i<=n;i++)
   {
       if(i%2==0)
       cout<<c++;
       for(int j=1;j<=n;j++)
       {
           cout<<i;
       }
       if(i%2!=0)
       cout<<c++;
       cout<<"\n";
   }
 
 
   return 0;
}

11 comments:

  1. can u pls give the c code with this program

    ReplyDelete
  2. #include
    int main(void) {
    int i,j,n=3,c=n-1;
    for(i=1;i<=n;i++)
    {
    if(i%2==0)
    printf("%d",c++);
    for(j=1;j<=n;j++)
    {
    printf("%d",i);
    }
    if(i%2!=0)
    printf("%d",c++);
    printf("\n");
    }

    return 0;
    }

    ReplyDelete
    Replies
    1. Is this program is for printing 1112
      3222
      3334
      In C LANGUAGE??

      Delete
    2. /******************************************************************************

      Online C Compiler.
      Code, Compile, Run and Debug C program online.
      Write your code in this editor and press "Run" button to compile and execute it.

      *******************************************************************************/

      #include

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

      return 0;
      }

      Delete
  3. this program works only for n=3
    if you want to work for n numbers put c=2

    ReplyDelete
  4. this program is showing incorrect o/p

    ReplyDelete
  5. #include
    int main(void) {
    int i,j,n=3,c=n-1,rows;
    printf("Enter the number of rows");
    scanf("%d",&rows);
    for(i=1;i<=rows;i++)
    {
    if(i%2==0)
    printf("%d",c++);
    for(j=1;j<=n;j++)
    {
    printf("%d",i);
    }
    if(i%2!=0)
    printf("%d",c++);
    printf("\n");
    }

    return 0;
    }

    ReplyDelete
  6. For more similar coding questions of amcat visit http://www.qsoln.com/amcat-coding/

    ReplyDelete
  7. #include
    void main()
    {
    int i,j,n,k;
    printf("Enter the nos line u want to print :");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=4;j++)
    {
    if(i%2)
    {
    if(j<4)
    printf("%d",i);
    else{
    k=i;
    printf("%d",++k);
    }
    }
    else{
    if(j==1)
    {
    k=i;
    printf("%d",++k);
    }
    else
    printf("%d",i);
    }
    }
    printf("\n");
    }
    }

    ReplyDelete
  8. That was a easy logic to solve but someone is saying to put c=2 its not 2 sir its ihe value just befire the value of n thatis n-1 thatswhy c=n-1 if u will put c=2 the the output will be same for every input

    ReplyDelete