How to print an Even Numbers in Upper Triangular of a matrix and Odd Numbers in Lower Triangular of a Matrix and top-left to bottom-right diagonal should be zero. In this programming problem, we have to perform multiple tasks, we need to implement a multiple logic Here is the list of the task we have to perform while solving this problem.

This below pic explain everything in a simple language. See this image carefully you will get all the picture of the pattern problem in Java language. You can see that I have colored all three part for clarifying the problem in an easy way, Here you can see that the Upper Triangular with red color and Lower triangular with Blue color and a diagonal with Green color. So Now we got the problem we have to now find the solution by applying the above 3 programming logic. Before starting let again take a look in a simple way.

0 2 4 6 8 10

1 0 12 16 18 20

3 5 0 22 24 26

7 11 13 0 28 30

17 19 23 29 0 32

31 37 41 43 47 0

0 2 4 6 8 10

0 12 16 18 20

0 22 24 26

0 28 30

0 32

0

0

1 0

3 5 0

7 11 13 0

17 19 23 29 0

31 37 41 43 47 0

Here is a complete solution up to 18 digit number, this program can go up to any number of for better viewing output in a console screen I suggest use only up to 15 number. You can increase a limit by updating the value of an array(Increase the size of an array).

import java.util.Scanner;

import java.util.Arrays;

class DiagonalEvenOdd

{

public static void main(String[] args)

{

Scanner sc =new Scanner(System.in);

System.out.print("Enter the Value Between 1-15\n\n");

System.out.print("Enter the Number: ");

int number=sc.nextInt();

pattern(number);

}

public static void pattern(int number)

{

int row=number;

int column=number;

int evenNum=2, primeNum=1;

int h=0, i=0, j=0, y=0, count=0;

int[] tempArr=null;

int[][] arr=new int[row][column];

for(i=0;i<row;i++)

{

for(j=0;j<column;j++)

{

if(i==j)

{

arr[i][j]=0;

}

if(j>i)

{

if(evenNum==14)

{

evenNum+=2;

}

arr[i][j]=evenNum;

evenNum+=2;

}

if(j<i)

{

tempArr=new int[500];

y=0;

for(int p=1;p<=1000;p++)

{

count=0;

for(int q=1;q<=1000;q++)

{

if(p%q==0)

{

count++;

}

}

if(count<=2)

{

if(p==2)

{

continue;

}

tempArr[y]=p;

y++;

}

}

arr[i][j]=tempArr[h];

h++;

}

}

}

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

for(i=0;i<row;i++)

{

for(j=0;j<column;j++)

{

System.out.print(arr[i][j]+"\t");

}

System.out.println();

}

}

}

- We have to check number is Even or Odd
- We should know how to print an upper and lower triangular in the matrix
- We should know the 2D array knowledge to implement a matrix

This below pic explain everything in a simple language. See this image carefully you will get all the picture of the pattern problem in Java language. You can see that I have colored all three part for clarifying the problem in an easy way, Here you can see that the Upper Triangular with red color and Lower triangular with Blue color and a diagonal with Green color. So Now we got the problem we have to now find the solution by applying the above 3 programming logic. Before starting let again take a look in a simple way.

**If the number value is 6 then we got we got a this output:**0 2 4 6 8 10

1 0 12 16 18 20

3 5 0 22 24 26

7 11 13 0 28 30

17 19 23 29 0 32

31 37 41 43 47 0

**Now upper triangular with even number line by line and with zero diagonal**0 2 4 6 8 10

0 12 16 18 20

0 22 24 26

0 28 30

0 32

0

**Now Lower triangular with odd number line by line and with zero diagonal**0

1 0

3 5 0

7 11 13 0

17 19 23 29 0

31 37 41 43 47 0

Here is a complete solution up to 18 digit number, this program can go up to any number of for better viewing output in a console screen I suggest use only up to 15 number. You can increase a limit by updating the value of an array(Increase the size of an array).

### Solution to Print Even Numbers in Upper Triangular and Odd Numbers in Lower Triangular

import java.util.Scanner;

import java.util.Arrays;

class DiagonalEvenOdd

{

public static void main(String[] args)

{

Scanner sc =new Scanner(System.in);

System.out.print("Enter the Value Between 1-15\n\n");

System.out.print("Enter the Number: ");

int number=sc.nextInt();

pattern(number);

}

public static void pattern(int number)

{

int row=number;

int column=number;

int evenNum=2, primeNum=1;

int h=0, i=0, j=0, y=0, count=0;

int[] tempArr=null;

int[][] arr=new int[row][column];

for(i=0;i<row;i++)

{

for(j=0;j<column;j++)

{

if(i==j)

{

arr[i][j]=0;

}

if(j>i)

{

if(evenNum==14)

{

evenNum+=2;

}

arr[i][j]=evenNum;

evenNum+=2;

}

if(j<i)

{

tempArr=new int[500];

y=0;

for(int p=1;p<=1000;p++)

{

count=0;

for(int q=1;q<=1000;q++)

{

if(p%q==0)

{

count++;

}

}

if(count<=2)

{

if(p==2)

{

continue;

}

tempArr[y]=p;

y++;

}

}

arr[i][j]=tempArr[h];

h++;

}

}

}

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

for(i=0;i<row;i++)

{

for(j=0;j<column;j++)

{

System.out.print(arr[i][j]+"\t");

}

System.out.println();

}

}

}