JAVA_CODINGS Telegram 61
42. Add two Matrix.
import java.util.*;

class AddTwoMatrix
{
int m, n;
int first[][] = new int[m][n];
int second[][] = new int[m][n];

AddTwoMatrix(int[][] first, int[][] second, int m, int n)
{
this.first = first;
this.second = second;
this.m = m;
this.n = n;
}

public static void main(String[] args)
{
int m, n, c, d;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];
int second[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
first[c][d] = in.nextInt();
}
}

System.out.println("Enter the elements of second matrix");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
second[c][d] = in.nextInt();
}
}

System.out.println("\nElements of First matrix is : ");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(first[c][d] + "\t");
}
System.out.println();
}

System.out.println("\nElements of Second matrix is : ");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(second[c][d] + "\t");
}
System.out.println();
}

AddTwoMatrix a = new AddTwoMatrix(first, second, m, n);
//call by reference
a.addmatrix(a); //Passing Object
}

//Function for Adding two matrix and storing in third matrix
public void addmatrix(AddTwoMatrix a)
{
int c, d;
int sum[][] = new int[a.m][a.n];

for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
sum[c][d] = a.first[c][d] + a.second[c][d];
}
}

System.out.println("\nSum of the two matrices is : ");

for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
System.out.print(sum[c][d] + "\t");
}
System.out.println();
}
}
}

@java_codings



tgoop.com/java_codings/61
Create:
Last Update:

42. Add two Matrix.
import java.util.*;

class AddTwoMatrix
{
int m, n;
int first[][] = new int[m][n];
int second[][] = new int[m][n];

AddTwoMatrix(int[][] first, int[][] second, int m, int n)
{
this.first = first;
this.second = second;
this.m = m;
this.n = n;
}

public static void main(String[] args)
{
int m, n, c, d;

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];
int second[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
first[c][d] = in.nextInt();
}
}

System.out.println("Enter the elements of second matrix");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
second[c][d] = in.nextInt();
}
}

System.out.println("\nElements of First matrix is : ");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(first[c][d] + "\t");
}
System.out.println();
}

System.out.println("\nElements of Second matrix is : ");

for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
System.out.print(second[c][d] + "\t");
}
System.out.println();
}

AddTwoMatrix a = new AddTwoMatrix(first, second, m, n);
//call by reference
a.addmatrix(a); //Passing Object
}

//Function for Adding two matrix and storing in third matrix
public void addmatrix(AddTwoMatrix a)
{
int c, d;
int sum[][] = new int[a.m][a.n];

for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
sum[c][d] = a.first[c][d] + a.second[c][d];
}
}

System.out.println("\nSum of the two matrices is : ");

for (c = 0; c < a.m; c++)
{
for (d = 0; d < a.n; d++)
{
System.out.print(sum[c][d] + "\t");
}
System.out.println();
}
}
}

@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Share with your friend now:
tgoop.com/java_codings/61

View MORE
Open in Telegram


Telegram News

Date: |

Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. 1What is Telegram Channels? Telegram iOS app: In the β€œChats” tab, click the new message icon in the right upper corner. Select β€œNew Channel.” The Standard Channel Clear
from us


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM American