Sum of Left Diagonal Elements of Matrix Java

Sum of Left Diagonal Elements of Matrix Java

Sum of Left Diagonal Elements of Matrix Java

Here is  the Java Program To Find Sum of Left Diagonal Elements of Matrix

We are uploading these types of program. However, if you need fully compiled solution of any type of Java program then simply leave a comment below, we will help you at the earliest. 😊😊

 import java.util.*;  
 public class Sum_of_Left_Diagonal  
 {  
   public static void main()  
   {  
     int s=0;  
     Scanner sc=new Scanner(System.in);  
     System.out.println("Enter the number of Rows");  
     int r=sc.nextInt();  
     System.out.println("Enter the number of Columns");  
     int c=sc.nextInt();  
     int arr[][]=new int[r][c];  
     System.out.println("Enter the elements of Matrix");  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
         arr[i][j]=sc.nextInt();  
       }  
     }  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
         System.out.print(arr[i][j]+" ");   
       }  
       System.out.println("");  
     }  
     for(int i=0;i<r;i++)  
     {  
       for(int j=0;j<c;j++)  
       {  
        if(i==j)  
         s=s+arr[i][j];  
       }  
     }  
       System.out.println("Sum of Left Diagonal Elements="+s);  
     }  
   }  
 ------------  
 OUTPUT:-   
 ------------  
 Enter the number of Rows  
 3  
 Enter the number of Columns  
 3  
 Enter the elements of Matrix  
 1  
 2  
 3  
 6  
 5  
 4  
 7  
 5  
 6  
 1 2 3   
 6 5 4   
 7 5 6  
 Sum of Left Diagonal Elements = 12  
 ----------------------------------  

Checkout:- Sort Matrix in Ascending Order Java

Checkout:-  Sorting Rows of Matrix in Descending Order Java

Post a Comment

0 Comments