Advertisement
Waliullah8328

Matrix Diagonal, Upper Triangle, Lower Triangle

Jan 28th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Diagonal {
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int row,col;
  9.         int m,n;
  10.         int sum1 = 0 ,sum = 0,sum2 = 0;
  11.  
  12.         System.out.print("Enter how many row you want to insert: ");
  13.         m = input.nextInt();
  14.         System.out.print("Enter how many Colum you want to insert: ");
  15.         n = input.nextInt();
  16.         int [][] A = new int [m][n];
  17.         System.out.println("------------------- Enter your "+(m*n)+" digit ---------------------------- ");
  18.         System.out.println();
  19.         for (row = 0; row < m ; row++)
  20.         {
  21.             for(col = 0; col< n; col++)
  22.             {
  23.                 System.out.printf("Enter your A[%d][%d] number : ",row,col);
  24.                 A[row][col]= input.nextInt();
  25.             }
  26.         }
  27.         System.out.println();
  28.         System.out.println("Your Matrix:");
  29.         for (row = 0; row < m ; row++)
  30.         {
  31.             for(col= 0; col< n; col++)
  32.             {
  33.                 System.out.print(" "+A[row][col]);
  34.             }
  35.             System.out.println();
  36.         }
  37.         System.out.println();
  38.         System.out.println("Diagonal list :");
  39.         for (row = 0; row < m ; row++)
  40.         {
  41.             for(col = 0; col< n; col++)
  42.             {
  43.                 if( row == col)
  44.                 {
  45.                     System.out.print(" "+A[row][col]);
  46.                     sum = sum + A[row][col];
  47.                 }
  48.  
  49.             }
  50.  
  51.         }
  52.         System.out.println();
  53.         System.out.print("Sum of Diagonal : "+sum);
  54.  
  55.         System.out.printf("\n\n");
  56.         System.out.println("Upper Triangle :");
  57.         for (row = 0; row < m ; row++)
  58.         {
  59.             for(col = 0; col < n; col++)
  60.             {
  61.                 if( row < col)
  62.                 {
  63.                     System.out.print(" "+A[row][col]);
  64.                     sum1 = sum1 + A[row][col];
  65.                 }
  66.  
  67.             }
  68.  
  69.         }
  70.         System.out.println();
  71.         System.out.println("Sum of Upper Triangle : "+sum1);
  72.  
  73.         System.out.println();
  74.         System.out.println("Lower Triangle :");
  75.         for (row = 0; row < m ; row++)
  76.         {
  77.             for(col = 0; col < n; col++)
  78.             {
  79.                 if( row > col)
  80.                 {
  81.                     System.out.print(" "+A[row][col]);
  82.                     sum2 = sum2 + A[row][col];
  83.                 }
  84.  
  85.             }
  86.  
  87.         }
  88.         System.out.println();
  89.         System.out.println("Sum of Lower Triangle : "+sum2);
  90.  
  91.  
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement