0% found this document useful (0 votes)
21 views

Java Matrix Problem

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java Matrix Problem

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.

java;
import java.util.Scanner;

public class Matrix {

public static void main(String[] args) {


// TODO Auto-generated method stub
int p,q,m,n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix");
p=sc.nextInt();
System.out.print("Enter the number of coulmns in the first matrix");
q=sc.nextInt();
System.out.print("Enter the number of rows in the second matrix");
m=sc.nextInt();
System.out.print("Enter the number of columns in the second matrix");
n=sc.nextInt();

if(p==m && q==n)


{
int a[][]=new int[p][q];
int b[][]=new int[m][n];
int c[][]=new int[p][q];

System.out.print("Enter all the elements of first matrix:");

for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("");

System.out.print("Enter all the elements of second matrix:");

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("");

System.out.println("First matrix");

for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}

System.out.println("Second matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}

for(int i=0;i<p;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<q;k++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
System.out.println("Matrix after addition");
for(int i=0;i<p;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
sc.close();
}
else
{
System.out.println("addition not possible");
System.out.println("Try again");
}
}

You might also like