Array-promo
Matrix-promo

Question

program to non-diagonal element of matrix

				
					Matrix  = [9, 8, 7]
          [6, 5, 4]
          [3, 2, 1]
          
          
NON-DIAGONAL ELEMENT OF MATRIX
   8   
6     4
   2 
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class NonDiagonalElementOfMatrix  
{  
    public static void main(String[] args)
    {  
        int size=0,i=0,j=0;  
        int a[][];
        
        
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE SIZE OF MATRIX");
        size=sc.nextInt();
        
        a=new int[size][size];
        System.out.println("ENTER THE ELEMENTS IN MATRIX");
        for(i=0;i< size;i++)
        {
            for(j=0;j< size;j++)
            {
                a[i][j]=sc.nextInt();
            }
        }
            
        System.out.println("NON-DIAGONALS ELEMENTS OF MATRIX");

        for(i=0;i< size;i++)
        {
            for(j=0;j< size;j++)
            {
                
                if(i!=j && (i+j!=size-1))
                {
                    System.out.print(a[i][j]+" ");
                    
                }
                else
                {
                    System.out.print("  ");
                }
                    
            }
                System.out.println();
        }
            
    }  
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *