Write a program to print the diagonal elements of 3X3 matrix .

class Dmatrix
{
 public static void main(String arg[])
 {
  int a[][]= new int[3][3];
  int i,j,k=0;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    a[i][j]=k;
     k++;
   }
  }
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +a[i][j]);
         
     }
     System.out.println("\n");
    }
System.out.println("digo. elment are ");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i!=j)
     System.out.print(" ");
     else
     System.out.println(" "+a[i][j]);
     }
    }
 }
}
 

2 comments:

  1. output is like this
    digo. elment are
    0
    4
    8

    was the output should be like this

    ReplyDelete
  2. it print only one diagonal...what about 02 20 index

    ReplyDelete