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.
- 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
Output:
Enter the Value Between 1-15
Enter the Number: 10
0 2 4 6 8 10 12 16 18 20
1 0 22 24 26 28 30 32 34 36
3 5 0 38 40 42 44 46 48 50
7 11 13 0 52 54 56 58 60 62
17 19 23 29 0 64 66 68 70 72
31 37 41 43 47 0 74 76 78 80
53 59 61 67 71 73 0 82 84 86
79 83 89 97 101 103 107 0 88 90
109 113 127 131 137 139 149 151 0 92
157 163 167 173 179 181 191 193 197 0
need an explanation for this answer? contact us directly to get an explanation for this answer