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

Source Code: 13.write A Program To Show Addition, Subtraction and Multiplication of Two Matrices

The document contains code to perform several operations on arrays in Java including: 1) Adding, subtracting and multiplying matrices by taking input from the user and performing the specified operation on corresponding elements. 2) Finding the sum and average of integers in an array using an enhanced for loop. 3) Sorting an array of integers in ascending order using nested for loops.

Uploaded by

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

Source Code: 13.write A Program To Show Addition, Subtraction and Multiplication of Two Matrices

The document contains code to perform several operations on arrays in Java including: 1) Adding, subtracting and multiplying matrices by taking input from the user and performing the specified operation on corresponding elements. 2) Finding the sum and average of integers in an array using an enhanced for loop. 3) Sorting an array of integers in ascending order using nested for loops.

Uploaded by

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

13.

Write a program to show addition, subtraction and multiplication of


two matrices.

Source Code

import java.util.Scanner;
public class Matrix{
public static int[][] inputArray(int m, int n) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements of the matrice");
int arr[][]=new int[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
arr[i][j]=in.nextInt();
}
}
return arr;
}
public static int[][] add(int[][] arr, int[][] arr1, int m, int n) {
int arr2[][]=new int[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
arr2[i][j]=arr[i][j]+arr1[i][j];
}
}
return arr2;
}

public static int[][] sub(int[][] arr, int[][] arr1, int m, int n) {


int arr2[][]=new int[m][n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
arr2[i][j]=arr[i][j]-arr1[i][j];
}
}
return arr2;
}

public static int[][] mul(int[][] arr, int[][] arr1, int m, int n, int
m1, int n1) {
int arr2[][]=new int[m][n1];
for(int i=0;i<m;i++) {
for(int j=0;j<n1;j++) {
arr2[i][j]=0;
for(int k=0;k<n;k++) {
arr2[i][j]+=arr[i][k]*arr1[k][j];
}
}
}
return arr2;
}

public static void printArray(int[][] arr, int m, int n) {


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

public static void main(String[] args) {


int m, n, m1, n1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row size of the 1st matrice");
m=sc.nextInt();
System.out.println("Enter the column size of the 1st matrice");
n=sc.nextInt();
int arr[][]=new int[m][n];
arr=inputArray(m, n);
System.out.println("Enter the row size of the 2nd matrice");
m1=sc.nextInt();
System.out.println("Enter the column size of the 2nd matrice");
n1=sc.nextInt();
int arr1[][]=new int[m1][n1];
arr1=inputArray(m1, n1);
System.out.println("Options:-");
System.out.println("1.Matrice Addition");
System.out.println("2.Matrice Substraction");
System.out.println("3.Matrice Multiplication");
int c=sc.nextInt();
if(c==1) {
if((m==m1)&&(n==n1)) {
int arr2[][]=new int[m][n];
arr2=add(arr, arr1, m, n);
System.out.println("The Result:-");
printArray(arr2, m, n);
}
else {
System.out.println("Matrice Addition is not
possible");
}
}
else if(c==2) {
if((m==m1)&&(n==n1)) {
int arr2[][]=new int[m][n];
arr2=sub(arr, arr1, m, n);
System.out.println("The Result:-");
printArray(arr2, m, n);
}
else {
System.out.println("Matrice Substraction is not
possible");
}
}
else if(c==3) {
if(n==m1) {
int arr2[][]=new int[m][n1];
arr2=mul(arr, arr1, m, n, m1, n1);
System.out.println("The Result:-");
printArray(arr2, m, n1);
}
else {
System.out.println("Matrice Multiplication is not
possible");
}
}
else {
System.out.println("Wrong Choice");
}
}
}

Output

Enter the row size of the 1st matrice


3
Enter the column size of the 1st matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Enter the row size of the 2nd matrice
3
Enter the column size of the 2nd matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Options:-
1.Matrice Addition
2.Matrice Substraction
3.Matrice Multiplication
1
The Result:-
2 4 6
8 10 12
14 16 18

Enter the row size of the 1st matrice


3
Enter the column size of the 1st matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Enter the row size of the 2nd matrice
3
Enter the column size of the 2nd matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Options:-
1.Matrice Addition
2.Matrice Substraction
3.Matrice Multiplication
2
The Result:-
0 0 0
0 0 0
0 0 0

Enter the row size of the 1st matrice


3
Enter the column size of the 1st matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Enter the row size of the 2nd matrice
3
Enter the column size of the 2nd matrice
3
Enter the elements of the matrice
1
2
3
4
5
6
7
8
9
Options:-
1.Matrice Addition
2.Matrice Substraction
3.Matrice Multiplication
3
The Result:-
30 36 42
66 81 96
102 126 150

14.Write a program to find sum and average of several integers (in an


array) using enhanced-for loop.

Source Code

import java.util.Scanner;
public class Program14 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int arr[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
} int sum=0;
double average=0;
for(int i:arr) {
sum+=i;
}
average=(double)sum/n;
System.out.println("Sum:"+sum);
System.out.printf("Average:%.2f", average);
}
}
Output
Enter the size of the array
5
Enter the array elements
1
2
3
4
5
Sum:15
Average:3.00

Array Sort

Source code

import java.util.Scanner;
public class Arraysort {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int arr[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
} for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("The sorted array");
for(int i:arr)
{
System.out.print(i+" ");
}
System.out.println();
}
}
Output
Enter the size of the array
5
Enter the array elements
5
4
3
2
1
The sorted array
1 2 3 4 5

You might also like