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

2D Array ProgramesUpdate

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

2D Array ProgramesUpdate

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

2D Array Programes

1.Create a 2Darray?
package Array2D;

import java.util.Scanner;

public class CreateArray {

public static void main(String[] args) {

Scanner scn=new Scanner(System.in);

System.out.println("Enter a row");

int row=scn.nextInt();

System.out.println("Enter a rcol");

int col=scn.nextInt();

int a[][]=new int[row][col];

for(int i=0;i<a.length;i++)

System.out.println("Enter the data for rown no "+(i+1));

for(int j=0;j<a[i].length;j++)

a[i][j] = scn.nextInt();

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

System.out.print(a[i][j]+" ");
}

System.out.println();

O/P
Enter a row

Enter a rcol

Enter the data for rown no 1

Enter the data for rown no 2

Enter the data for rown no 3

1 2 3

4 5 6

7 8 9

2.Creating an array char array?


package Array2D;

import java.util.Scanner;

public class CreatingArrayChar {

public static void main(String[] args) {

Scanner scn=new Scanner(System.in);

System.out.println("Enter a row");

int row=scn.nextInt();

System.out.println("Enter a rcol");

int col=scn.nextInt();

char a[][]=new char[row][col];

for(int i=0;i<a.length;i++)

System.out.println("Enter the data for rown no "+(i+1));

for(int j=0;j<a[i].length;j++)

a[i][j] =scn.next().charAt(0);

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

System.out.print(a[i][j]+" ");

System.out.println();
}

O/P
Enter a row

Enter a rcol

Enter the data for rown no 1

Enter the data for rown no 2

Enter the data for rown no 3

a s d

f g h

h j k

3. Creating an array Double array?


package Array2D;

import java.util.Scanner;

public class CreatingArraydouble {

public static void main(String[] args) {

Scanner scn=new Scanner(System.in);

System.out.println("Enter a row");

int row=scn.nextInt();

System.out.println("Enter a rcol");

int col=scn.nextInt();

double a[][]=new double[row][col];

for(int i=0;i<a.length;i++)

System.out.println("Enter the data for rown no "+(i+1));

for(int j=0;j<a[i].length;j++)

a[i][j] = scn.nextInt();

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

System.out.print(a[i][j]+" ");

System.out.println();
}

O/P

Enter a row

Enter a rcol

Enter the data for rown no 1

10

20

30

Enter the data for rown no 2

40

50

60

Enter the data for rown no 3

70

80

90

10.0 20.0 30.0

40.0 50.0 60.0

70.0 80.0 90.0

4.Write a Program Biggest Element of Each Row?


package Array2d27;

public class BiggestElementEachRow {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

int bg;

for(int i=0;i<a1.length;i++)

bg=a1[i][0];

for(int j=0;j<a1[i].length;j++)

if(bg<a1[i][j]) bg=a1[i][j];

System.out.print(bg+" ");

System.out.println();

O/P

30 60 90
5.Write a Program Biggest Element of Each col?

package Array2d27;

public class BiggestElementEachRow {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

int bg;
for(int i=0;i<a1.length;i++)

bg=a1[i][0];

for(int j=0;j<a1[i].length;j++)

if(bg<a1[j][i]) bg=a1[j][i];

System.out.print(bg+" ");

System.out.println();

O/P

70 80 90

6.Compare 2D array Matrix same or not?

package Array2d27;

public class Compare2DMatrixSameOrNot {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

int[][] a2= {{10,20,30},{40,50,60},{70,80,90}};

boolean flag=true;

for(int i=0;i<a1.length;i++)

for(int j=0;j<a1[i].length;j++)

{
if(a1[i][j]!=a2[i][j])

flag=false;

break;

if(flag)

System.out.println("Two arrays are same");

else

System.out.println("2arrays are not same ");

O/P

Two arrays are same

7.Print arrya Even are Odd Elements count in 2D array?


package Array2d27;

public class EvenOrOddElementsArray {

public static void main(String[] args) {

int[][] a= {{1,2,3},{4,5,6},{7,8,9}};

int even = 0;
int odd = 0;

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

if(a[i][j]%2==0) even++;

else odd++;

System.out.println(even+" even numbers");

System.out.println(odd+" odd numbers");

O/P

4 even numbers

5 odd numbers
8. Take a matrix input and check if it is identity matrix?

package Array2d27;

import java.util.Scanner;

public class IdentityMatrix {

public static void main(String[] args) {

int arrd[][] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };

boolean flag = true;

for (int i = 0; i < arrd.length; i++)

{
for (int j = 0; j < arrd[i].length; j++)

if(i==j)

if(arrd[i][j]==1)

{}

else {

flag=false;

break;

else if(arrd[i][j]!=0)

flag=false;

break;

if (flag)

System.out.println("Identity Matrix");

else

System.out.println("Not a Identity Matrix");

}
O/P

Identity Matrix

9.Print the Lower Triangle Program?

package Array2d27;

public class LowerTriangleMatrix {

public static void main(String[] args) {

int[][] a= {{1,2,3},{4,5,6},{7,8,9}};

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

if(i<j)

System.out.print("0"+" ");

else

System.out.print(a[i][j]+" ");

System.out.println();

}
O/P
1 0 0

4 5 0

7 8 9

10.Print the Upper Triangle?


package Array2d27;

public class UpperTriangle {

public static void main(String[] args) {

int[][] a= {{1,2,3},{4,5,6},{7,8,9}};

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

if(i>j)

System.out.print("0"+" ");

else

System.out.print(a[i][j]+" ");

System.out.println();

}
}

O/P

123

056

009

11.Write a Program Matrix Addition?


package Array2d27;

public class MatrixAddition {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

int[][] a2= {{10,20,30},{40,50,60},{70,80,90}};

int[][] sum=new int[3][3];

for(int i=0;i<sum.length;i++)

for(int j=0;j<sum.length;j++)

sum[i][j]=a1[i][j]+a2[i][j];

System.out.print(sum[i][j]+" ");

System.out.println();

O/P

20 40 60
80 100 120

140 160 180

12.Write a program Matrix Multiplication?


package Array2d27;

import java.util.Scanner;

public class MatrixMultiplication {

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter The row size..");

int row=sc.nextInt();

System.out.println("Enter The Col Size..");

int col=sc.nextInt();

int arr[][]=new int[row][col];

for(int i=0;i<row;i++)

System.out.println("Enter The data for Row No "+(i+1));

for(int j=0;j<col;j++)

arr[i][j]=sc.nextInt();

System.out.println("Printing The 1st Array Elements..");

for(int i=0;i<row;i++)

{
for(int j=0;j<col;j++)

System.out.print(arr[i][j]+" ");

System.out.println();

System.out.println("Enter The 2nd Array row size..");

int row1=sc.nextInt();

System.out.println("Enter The 2nd Array The Col Size..");

int col1=sc.nextInt();

int arr1[][]=new int[row1][col1];

for(int i=0;i<row1;i++)

System.out.println("Enter The data for Row No "+(i+1));

for(int j=0;j<col1;j++)

arr1[i][j]=sc.nextInt();

System.out.println("Printing The 2nd Array Elements..");

for(int i=0;i<row1;i++)

for(int j=0;j<col1;j++)

System.out.print(arr1[i][j]+" ");

System.out.println();
}

int [][] multi = new int [row][col];

for(int i = 0 ; i < row ; i++)

for(int j = 0 ; j < col ; j++)

for(int k=0;k<col;k++)

multi [i][j] += arr[i][k] * arr1[k][j];

System.out.println();

for(int i=0;i<row;i++)

for(int j=0;j<col;j++)

System.out.print(multi[i][j]+" ");

System.out.println();

Another way.

package Programms;

import java.util.Arrays;

public class MatrixMultiplication {


public static void main(String[] args) {
int[][]a= {{1,2},{3,4}};
int[][]b= {{5,6},{7,8}};
int[][]c=new int[a[0].length][b.length];
if(a.length==b.length)
{
for(int r1=0;r1<a.length;r1++)
{
for(int c2=0;c2<b[r1].length;c2++)
{
for(int c1=0;c1<a[r1].length;c1++)
{
c[r1][c2]+=a[r1][c1]*b[c1][c2];
}
}
}

System.out.println(Arrays.deepToString(c));

}
}

O/P
Enter The row size..

Enter The Col Size..

Enter The data for Row No 1

Enter The data for Row No 2

Printing The 1st Array Elements..

12
34

Enter The 2nd Array row size..

Enter The 2nd Array The Col Size..

Enter The data for Row No 1

Enter The data for Row No 2

Printing The 2nd Array Elements..

56

67

17 20

39 46

13.Write a Program Maximum Value Of a 2D Array?


package Array2d27;

public class MaxValueOf2DArray {

public static void main(String[] args) {

int[][] a1= {{90,20,30},{40,150,60},{70,80,30}};

int bg = a1[0][0];

for(int i=0;i<a1.length;i++)

for(int j=0;j<a1[i].length;j++)

{
if(bg<a1[i][j]) bg=a1[i][j];

System.out.print(bg+" ");

O/P

150

14.Write a Program SparseMatrix or Not?


package Array2d27;

public class SparceMatrix {

public static void main(String[] args) {

int[][] a= {{4,6,0},{0,0,2},{3,0,0}};

int count=0;

int num=0;

for(int i=0;i<a.length;i++)

for(int j=0;j<a[i].length;j++)

if(a[i][j]==0) count++;

if(a[i][j]!=0) num++;

}
if(count>num)

System.out.println("Sparse");

else

System.out.println("Not Sparse");

O/P

Sparse

15.Write a Program Subtarction Matrix Or Not?


package Array2d27;

public class SubMatrix {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

int[][] a2= {{1,2,3},{4,5,6},{7,8,9}};

int[][] sum=new int[3][3];

for(int i=0;i<sum.length;i++)

for(int j=0;j<sum.length;j++)
{

sum[i][j]=a1[i][j]-a2[i][j];

System.out.print(sum[i][j]+" ");

System.out.println();

O/P
9 18 27

36 45 54

63 72 81

16.Wirte a program sum of columns in 2D Array?


package Array2d27;

public class SumOfColumn {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

for(int i=0;i<a1.length;i++)

for(int j=0;j<a1[i].length;j++)

System.out.print(a1[i][j]+" ");

}
System.out.println();

for(int i=0;i<a1.length;i++)

int sum=0;

for(int j=0;j<a1[i].length;j++)

sum+=a1[j][i];

System.out.print(sum+" ");

O/P

10 20 30

40 50 60

70 80 90

120 150 180

17.Wirte a program sum of ROWS in 2D Array?


package Array2d27;

public class SumOfRowes {

public static void main(String[] args) {

int[][] a1= {{10,20,30},{40,50,60},{70,80,90}};

for(int i=0;i<a1.length;i++)

for(int j=0;j<a1[i].length;j++)

System.out.print(a1[i][j]+" ");

System.out.println();

for(int i=0;i<a1.length;i++)

int sum=0;

for(int j=0;j<a1[i].length;j++)

sum+=a1[i][j];

System.out.print(sum+" ");

O/P
10 20 30 = 60

40 50 60 = 150

70 80 90 = 240

18.Write a Program Transpose Matrix in 2D Array?(Rows To


Column and Column To Rows)

package Array2d27;

public class TransposeMatrix {

public static void main(String[] args) {

int[][] a= {{1,2,3},{4,5,6},{7,8,9}};

for(int i=0;i<a.length;i++)

for(int j=0;j<a.length;j++)

System.out.print(a[j][i]+" ");

System.out.println();

O/P
1 4 7

2 5 8

3 6 9
19. WAJP Spiral Maitrix?
package Programms;

public class Spiral_Matrix {


public static void main(String[] args) {
int [] [] a=spiral(5);

for(int [] t:a) {
for(int n:t) {
System.out.print(n+"\t");

}
System.out.println();
System.out.println();
}
}

static int [][] spiral(int size){


int [][] a=new int [size][size];
int r=0,c=-1;
char mov='r';
for(int i=1;i<=size*size;i++)
{
switch(mov) {
case 'r':c++;
a[r][c]=i;

if(c==size-1||a[r][c+1]!=0) mov = 'd';


break;

case 'd':r++;
a[r][c]=i;
if(r==size-1||a[r+1][c]!=0) mov='l';
break;

case 'l':c--;
a[r][c]=i;
if(c==0||a[r][c-1]!=0) mov='u';
break;

case 'u':r--;
a[r][c]=i;
if(r==0||a[r-1][c]!=0) mov = 'r';
break;

}
}
return a;
}

}
O/p
1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

20.WAJP TO PRINT PASCAL TRIANGLE?


package Programms;

import java.util.Arrays;

public class PascalTriangle {


public static void main(String[] args) {

int[][] a = pascalTriangle(5);
int spaces = 4;
for(int[] t: a)
{
for(int i=1;i<=spaces;i++) System.out.print("
");
for(int n:t)
{
System.out.print(n+" ");
}
System.out.println();
spaces--;
}

static int[][] pascalTriangle(int size) {


int[][] a = new int[size][size];
for(int i=0;i<a.length;i++)
{
a[i] = new int[i+1];

for(int j=0;j<a[i].length;j++)
{
if(j==0 || i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
}

return a;
}
}
O/p

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

21.WAJP To print Anti Daignal Sum?


package Programms;

public class Anti_Diagnal {

public static void main(String[] args) {


int[][] a = {{1,2,3},
{4,5,6},
{7,8,9}};
int sum = 0;
for(int i =0;i<a.length;i++)
{
sum+=a[i][i];
if(a.length%2!=0 && i==a.length/2)
{
continue;
}
sum+=a[i][a.length-1-i];
}
System.out.println(sum);
}

O/p
25

22.WAJP To Print Array Elements Decending Order.

package Programms;

import java.util.Arrays;
public class ArraysDecending {
public static void main(String[] args) {
int[][] a= {{1,2,3},{4,5,6},{7,8,9}};
for(int row=0;row<a.length;row++)
{
sort(a[row]);
}
}
private static void sort(int[] a) {
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

}
System.out.println(Arrays.toString(a));
}

O/p
[3, 2, 1]
[6, 5, 4]
[9, 8, 7]

You might also like