Q:

Java Program to Print Even Numbers in Upper Triangular and Odd Numbers in Lower Triangular in a Matrix

belongs to collection: Examples of Loop Programs using Java

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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();
  }
 }
}

 

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

total answers (1)

<< Java Program For Calculate Percentage of 5 Subject...