Sunteți pe pagina 1din 6

MATRIX OPERATIONS

import java.util.Scanner;
public class MatrixArray
{
public static void main(String[] args)
{
int option;
char ch='y';
int arr[]=new int[10];
MatrixOperations matrixOperations = new MatrixOperations();
Scanner input=new Scanner(System.in);
while(ch=='y' || ch== 'Y')
{
System.out.println("Enter ur choice");
System.out.println("1.Subtraction of Matrices");
System.out.println("2.Transpose of matrix");
option=input.nextInt();
switch (option)
{
case 1:
matrixOperations.subMatrix();
break;
case 2:
matrixOperations.transMatrix();
break;
casw 3:
matrixOperations.multiplicationMatrix();
default:
System.out.println("You entered Wrong Number");
break;
}
System.out.println("Do u want continue(Y/N):");
ch=input.next().charAt(0);
}
}

}
import java.util.Scanner;
public class MatrixOperations {
Scanner input= new Scanner(System.in);
public void subMatrix()
{
int matrix1[][]=new int[10][10];
int matrix2[][]=new int[10][10];
int sub[][]=new int[10][10];
System.out.println("Enter the Rows and Columns of First matrix");
int r1=input.nextInt();
int c1=input.nextInt();
System.out.println("Enter the Rows and Columns of Second matrix");
int r2=input.nextInt();
int c2=input.nextInt();
if(r1==r2 && c1==c2)
{
System.out.println("Enter the First matrix elements");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
System.out.println("Enter the Element:");
matrix1[i][j]=input.nextInt();
}
}
System.out.println("Enter the second matrix elements");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
System.out.println("Enter the Element:");
matrix2[i][j]=input.nextInt();
}
}
System.out.println("The Subtraction of 2 Matrices is");
for(int i=0;i<r1;i++)
{

for(int j=0;j<c1;j++)
{
sub[i][j]=matrix1[i][j]-matrix2[i][j];
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
System.out.print(sub[i][j]+"
}
System.out.println();
}

");

}
}
public void transMatrix()
{
int matrix[][] =new int[10][10];
System.out.println("Enter the Rows and Columns of
Matrix");
int r=input.nextInt();
int c=input.nextInt();
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("Enter the Element:");
matrix[i][j]=input.nextInt();
}
}
int trans[][]=new int[c][r];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
trans[j][i]=matrix[i][j];
}
}
System.out.println("You Entered Matrix is");
for(int i=0;i<r;i++)

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

");

}
System.out.println("The Transpose matrix is:");
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
System.out.print(trans[i][j]+" ");
}
System.out.println();
}
}
pblic class multiplicationMatrix{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = 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 number of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each
other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )

{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
//program for displaying the sum of array elements and sum of positive values and
negative values of array
import java.util.Scanner;
public class sumofvalues {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int[] arr1=new int[10];
int sum=0,possum=0,negsum=0;
for(int i=0;i<arr1.length;i++)
{
System.out.println("enter"+ (i+1)+"element =");
arr1[i]=input.nextInt();
System.out.println((i+1)+"element ="+arr1[i]);
sum=sum+arr1[i];
if(arr1[i]>0)
possum=possum+arr1[i];
else
negsum=negsum+arr1[i];
}

System.out.println("sum of array elements;"+sum);


System.out.println("sum of positive numbers in array"+possum);
System.out.println("sum of negative numbers of array"+negsum);
}
}

S-ar putea să vă placă și