Delzinho Postado Janeiro 14, 2015 Denunciar Share Postado Janeiro 14, 2015 import java. util Scanner; class Add Two Matrix { 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 sum [ ] [ ] = 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. next Int (); System.out. println (Enter the elements of second matrix") for ( c =0; c< m ; c++) for (d =0 ; d< n ; d++) sum [c][d] = first [c][d] + second [c][d]; // replace '+' with '-' to subtract matrices System. out. println ("Sum of entered matrices:-"); for ( c = 0; c < m; c++) { for ( d = 0; d<n ; d++) System.out print (sum[c][d] +"\t"); System. out. println (); } } } Out put Enter the number of rows and columns of matrix 2 2 Enter the elements of second matrix 4 3 2 1 Sum of entered matrices 5 5 5 5 Citar Link para o comentário Compartilhar em outros sites More sharing options...
Pergunta
Delzinho
import java. util Scanner;
class Add Two Matrix
{
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 sum [ ] [ ] = 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. next Int ();
System.out. println (Enter the elements of second matrix")
for ( c =0; c< m ; c++)
for (d =0 ; d< n ; d++)
sum [c][d] = first [c][d] + second [c][d]; // replace '+' with '-' to subtract matrices
System. out. println ("Sum of entered matrices:-");
for ( c = 0; c < m; c++)
{
for ( d = 0; d<n ; d++)
System.out print (sum[c][d] +"\t");
System. out. println ();
}
}
}
Out put
Enter the number of rows and columns of matrix 2 2
Enter the elements of second matrix 4 3
2 1
Sum of entered matrices
5 5
5 5
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.