Matrix - Algebra JAVA ISC
Matrix - Algebra JAVA ISC
Write a menu driven java program to find the sum, difference or product of two matrices
and displaying the resultant matrix.
ALGORITHM:
1. Start
2. Create class variables: m ,n and mm,nn to store columns and rows numbers of matrices a[][]
and arr[][] respectively.
3. Create matrices: a[][], arr[][], prod[][], sum[][], sum[][] and Sub[][].
4. Create static object sc of scanner class of java.util package to take user inputs.
5. Create a method void create().
6. Take input of column and row numbers of matrices a and arr from user.
7. Store the inputs in m,n and mm,nn respectively.
8. Declare the matrices a[][] and arr[][] with column and row numbers m,n and mm,nn.
9. Create method input() to take input of the elements of the matrix using scanner class.
10. To take input for matrix a[][] run a loop from int i=0 to i<m , incrementing i by 1 with a nested
loop running from j=0 to j<n, incrementing j by 1.
11. To take input for matrix arr[][] run a loop from int i=0 to i<mm , incrementing i by 1 with a
nested loop running from j=0 to j<nn, incrementing j by 1.
12. Create a method print_display with parameters as int m[][],int r and int c.
13. Print the matrix m[][] by running a loop from int i=0 to i<r ,incrementing i by 1 with a nested
loop running from j=0 to j<c, incrementing j by 1.
14. Create method display_original to display the original matrices.
15. Call the method print_display() and pass a,m,n as the arguments to print the first matrix.
16. Call the method print_display() and pass arr,mm,nn as the arguments to print the second
matrix.
17. Create method Mult() to multiply two matrices.
18. Declare matrix prod[][] with rows as m and columns as nn.
19. Run a for loop from i=0 to i<m and incrementing i by 1.
20. Run a nested for loop from j=0 to j<nn, increment j by 1.
21. Run a nested for loop from k=0 to k<mm, increment k by 1.
22. Store a[i][k]*arr[k][j] in prod[i][j].
23. To print the matrix prod[][] call the method print_display() and pass prod,m,nn as parameters.
24. Create method add() to add two matrices.
25. Declare sum[][] with m as rows and n as columns.
26. Run a for loop from i=0 to i<m and incrementing i by 1.
27. Run a for loop from j=0 to j<n and incrementing j by 1.
28. Store a[i][j] + arr[i]j] in sum[i][j].
29. To print the matrix sum[][] call the method print_display() and pass sum,m,n as parameters.
30. Create method diff() to subtract two matrices.
31. Declare Sub[][] with m as rows and n as columns.
32. Run a for loop from i=0 to i<m and incrementing i by 1.
33. Run a for loop from j=0 to j<n and incrementing j by 1.
34. Store a[i][j] - arr[i]j] in Sub[i][j].
35. To print the matrix Sub[][] call the method print_display() and pass Sub,m,n as parameters.
36. Create main method.
Page | 1
37. Create object ob of the class.
38. Give choices to the user:
1. Enter (1) for Multiplication
2. Enter (2) for Addition
3. Enter (3) for Subtraction
39. Store the choice of user in int ch.
40. If user enters 1, then call the method create() using object ob.
41. Check if n==mm.
42. If true, then call methods: input(),display_original(),Mult(), using the object ob.
43. If false, then print an output: Multiplicaton not possible.
44. If user enters 2, then call the method create() using object ob.
45. Check if m=mm and n=nn.
46. If true, then call methods: input(),display_original(),add(), using the object ob.
47. If false, then print an output: Addition not possible.
48. If user enters 3, then call the method create() using object ob.
49. Check if m=mm and n=nn.
50. If true, then call methods: input(),display_original(),diff(), using the object ob.
51. If false, then print an output: Subtraction not possible.
52. If the user input is anything else then print : Invalid Choice.
53. Stop.
SOURCE CODE:
import java.util.*;
public class Matrix_algebra
{
static int m,n,mm,nn;
int a[][];
int arr[][];
int prod[][];
int sum[][];
int Sub[][];
Page | 6
OUTPUT:
Page | 7
Page | 8
Page | 9
Page | 10