-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPost_Lab_2_Matrix.java
More file actions
89 lines (88 loc) · 2.5 KB
/
Post_Lab_2_Matrix.java
File metadata and controls
89 lines (88 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
public class Post_Lab_2_Matrix
{
public static void main(String Args[])
{
int row,col;
System.out.prinrtln("Enter the row and col: ");
row = s.nextInt();
col = s.nextInt();
int [][] matrix;
matrix = new int [row][col];
System.out.println("Enter the elements of matrix: ");
for(int i =0;i<row;i++)
{
System.out.println("Enter the elements of row "+(i+1));
for(int j =0;j<col;j++)
{
matrix[i][j] = s.nextInt();
}
}
while(true)
{
System.out.println("Select an option to perform below: ");
String srt = "1. Sum of all Elements:"+"\n2.Print the Matrix:"+"\n3. Print the diagonal of matrix: "+"\n4. Sum of Diagonal Elements"+"\n5. Exit()";
System.out.println(srt);
int opt;
System.out.println("Select an option: ");
opt = s.nextInt();
if(opt ==1)
{
int sum = 0;
for(int i=0;i<row;i++)
{
for(int j =0;j<col;j++)
{
sum+=matrix[i][j];
}
}
System.out.println("Sum of matrix: "+sum);
}
else if(opt ==2)
{
for(int i=0;i<row;i++)
{
for(int j =0;j<col;j++)
{
system.out.println(matrix[i][j]);
}
}
}
else if(opt ==3)
{
for(int i=0;i<row;i++)
{
for(int j =0;j<col;j++)
{
if(i==j)
{
System.out.println(matrix[i][j]);
}
}
}
}
else if(opt ==4)
{
int sum =0;
for(int i=0;i<row;i++)
{
for(int j =0;j<col;j++)
{
if(i==j)
{
sum+=matrix[i][j];
}
}
}
}
else if(opt==5)
{
break;
}
else
{
System.out.println("Enter a valid option");
}
}
}
}