Interview Question
Staff EngineersCountry: India
for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
if ((i % 2 != 0) && (j != n)) {
System.out.print(i + " ");
} else if ((i % 2 != 0) && (j == n)) {
System.out.print(i + 1 + " ");
} else if ((i % 2 == 0) && (j != 1)) {
System.out.print(i + " ");
} else if ((i % 2 == 0) && (j == 1)) {
System.out.print(i + 1 + " ");
}
}
System.out.println();
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
if ((i % 2 != 0) && (j != n)) {
System.out.print(i + " ");
} else if ((i % 2 != 0) && (j == n)) {
System.out.print(i + 1 + " ");
} else if ((i % 2 == 0) && (j != 1)) {
System.out.print(i + " ");
} else if ((i % 2 == 0) && (j == 1)) {
System.out.print(i + 1 + " ");
}
}
System.out.println();
}
void printPattern()
{
for(int i=1;i<m_n;i++ )
{
for(int j=0;j<m_n;j++)
{
if(i%2!=0){
if(j==m_n-1)
{
System.out.print((i+1)+" ");
System.out.println();
}else
{
System.out.print(i+" ");
}
}
else
{
if(j==0)
{
System.out.print((i+1)+" ");
}else
{
System.out.print(i+" ");
if(j==m_n-1)
{
System.out.println();
}
}
}
}
}
}
public static void main(String args[])
{
System.out.println("enter the number of rows");
Scanner s =new Scanner(System.in);
int num= s.nextInt();
for (int row = 1; row < num; row++) {
for (int col = 1; col <= num; col++) {
if(col==1)
System.out.print(row);
else if(col==num)
System.out.println(1+row);
else
System.out.print(row);
}
}
public static void main(String[] args) {
System.out.println("Enter the number of rows");
Scanner s =new Scanner(System.in);
int num= s.nextInt();
for(int j = 1 ;j<num;j++){
if(j%2 !=0){
for(int k= 0; k<7; k++){
System.out.print(j);
}
System.out.print(j+1);
}else{
System.out.print(j+1);
for(int k=0; k<7;k++){
System.out.print(j);
}
}
System.out.println();
}
}
import java.util.*;
class pattern
{
public static void main(String args[]){
int i,n,k,j;
Scanner sc=new Scanner(System.in);
System.out.println("enter size:");
n=sc.nextInt();
k=1;
for(i=0;i<n-1;i++)
{
if(i%2==0)
{
for(j=0;j<n-1;j++)
{
System.out.print(k);
}
System.out.println(k+1);
}
else
{
System.out.print(k+1);
for(j=0;j<n-1;j++)
{
System.out.print(k);
}
System.out.println();
}
k++;
}
}
}
public static void inPutmatx(int n, int l,int i) {
int j = n;
inputrec(n,l,i);
}
private static void inputrec(int n, int l,int i){
int j=n;
if (i == l - 1 && j < l) {
j = j + 1;
i = 0;
System.out.print(" " + j);
System.out.println();
inPutmatx(j, l,i);
} else {
if (j == l) {
System.out.print(" ");
} else {
i = i + 1;
System.out.print(" " + j);
inPutmatx(j, l,i);
//System.out.print(" " + j);
}
}
}
public static void inPutmatx(int n, int l,int i) {
int j = n;
inputrec(n,l,i);
}
private static void inputrec(int n, int l,int i){
int j=n;
if (i == l - 1 && j < l) {
j = j + 1;
i = 0;
System.out.print(" " + j);
System.out.println();
inPutmatx(j, l,i);
} else {
if (j == l) {
System.out.print(" ");
} else {
i = i + 1;
System.out.print(" " + j);
inPutmatx(j, l,i);
//System.out.print(" " + j);
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("size:");
int n=sc.nextInt();
boolean a = true;
for(int i=1;i<n;i++){
if(a){
printTrue(i,n);
a = false;
System.out.println("");
}else{
printFalse(i,n);
a = true;
System.out.println("");
}
}
}
private static void printTrue(int from, int to){
for(int i=1;i<to;i++){
System.out.print(from+" ");
}
System.out.print(from+1+" ");
}
private static void printFalse(int from, int to){
System.out.print(from+1+" ");
for(int i=1;i<to;i++){
System.out.print(from+" ");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("size:");
int n=sc.nextInt();
boolean a = true;
for(int i=1;i<n;i++){
if(a){
printTrue(i,n);
a = false;
System.out.println("");
}else{
printFalse(i,n);
a = true;
System.out.println("");
}
}
}
private static void printTrue(int from, int to){
for(int i=1;i<to;i++){
System.out.print(from+" ");
}
System.out.print(from+1+" ");
}
private static void printFalse(int from, int to){
System.out.print(from+1+" ");
for(int i=1;i<to;i++){
System.out.print(from+" ");
}
}
public class NumSeqFormat {
public static void main(String[] args) {
// TODO Auto-generated method stub
NumSeqFormat nf = new NumSeqFormat();
nf.printSeq(8);
}
public void printSeq(int c){
for(int i=1; i<c;i++){
int j =1;
if(i%2!=0){
while(j<c){
System.out.print(i+" ");
j++;
if(j==c){
System.out.print(i+1);
System.out.println("");
}
}
}else{
j=c;
if(j==c){
System.out.print(i+1+" ");
j--;
}
while(j>=1){
System.out.print(i+" ");
j--;
}
System.out.println("");
}
}
}
}
void generatePattern(const int number){
- Muhammed Ashiq January 25, 2017for(int row = 1; row < number ; row++){
for(int column = 1; column <= number; column++){
if(column == 1) {
if(row % 2 == 1 ) cout<<row<<" ";
else cout<<row+1<<" ";
} else if (column == number){
if(row % 2 == 1) cout<<row + 1<<" ";
else cout<<row<<" ";
} else {
cout<<row<<" ";
}
}
cout<<endl;
}
}