PrepSter Pattern Questions – 2

Also, if you want to study for AMCAT or MindTree and get previous questions in MindTree Quants, Verbal and Logical section, visit our –

Ques. Print the following Pattern and get the output to match test cases?

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

Program 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;
}

Program in C

#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;
}

Code in Java

public class Practice{
public static void main(String[] args){
PrintPat(3); }
public static void PrintPat(int a)
{ int n=1,i,j=1;
while(n<=a){
if(n%2!=0){
for(i=1;i<=a;i++)
System.out.print(n);

System.out.print(++j);
System.out.println();
}
else{
System.out.print(++j);
for(i=1;i<=a;i++)
System.out.print(n);
System.out.println();
}
n++;
}}}

Please do comment the code in other languages :).

For AMCAT Programming – Coding Questions Visit our AMCAT Previous Coding Dashboard 

One comment on “PrepSter Pattern Questions – 2”