0% found this document useful (0 votes)
32 views14 pages

18 Feb 2021 2d Programs

Uploaded by

ayush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

18 Feb 2021 2d Programs

Uploaded by

ayush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PROGRAMS ON

2D ARRAY
JAVA
Q1: ADDITION OF TWO MATRIX
import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int i, j;
int mat1[][] = new int[3][3];
int mat2[][] = new int[3][3];
int mat3[][] = new int[3][3];
Scanner scan = new Scanner(System.in);
System.out.print("Enter Matrix 1 Elements : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat1[i][j] = scan.nextInt();
}
}
CONTINUED….

System.out.print("Enter Matrix 2 Elements : ");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat2[i][j] = scan.nextInt();
}
}

System.out.print("Adding both Matrix to form the Third


Matrix...\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
System.out.print("The Two Matrix Added Successfully..!!\n");

System.out.print("The New Matrix will be :\n");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print(mat3[i][j]+ " ");
}
System.out.println();
}
}
}
Q2 :TRANSPOSE OF A MATRIX

import java.util.Scanner;

public class JavaProgram


{
public static void main(String args[])
{
int i, j;
int arr[][] = new int[3][3];
int arrt[][] = new int[3][3];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 3*3 Array Elements : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arr[i][j] = scan.nextInt();
}
}
CONTINUED...
System.out.print("Transposing Array...\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arrt[i][j] = arr[j][i];
}
}

System.out.print("Transpose of the Matrix is :\n");


for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.print(arrt[i][j]+ " ");
}
System.out.println();
}
}
}
Q3 :MULTIPLYING THE TWO MATRIX

To multiply two matrices in Java Programming, you have to first ask to the
user to enter the number of rows and columns of the first matrix and then
ask to enter the first matrix elements. Again ask the same for the second
matrix. Now start multiplying the two matrices and store the multiplication
result inside any variable say sum and finally store the value of sum in the
third matrix say multiply[ ][ ] at the equivalent index as shown in the
following program
CODE:

import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Rows and Columns of First Matrix : ");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];

System.out.print("Enter First Matrix Elements : ");

for(c=0 ; c<m; c++)


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

System.out.print("Enter Number of Rows and Columns of Second Matrix : ");


p = in.nextInt();
q = in.nextInt();
if ( n != p )
{
System.out.print("Matrix of the entered order can't be Multiplied..!!");
}
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.print("Enter Second Matrix Elements :\n");

for(c=0; c<p; c++)


{
for(d=0; d<q; d++)
{
second[c][d] = in.nextInt();
}
}
CONTINUED….

System.out.print("Multiplying both Matrix...\n");

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;
}
}
CONTINUED...

System.out.print("Multiplication Successfully performed..!!\n");


System.out.print("Now the Matrix Multiplication Result is
:\n");

for(c=0; c<m; c++)


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

}
}
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy