0% found this document useful (0 votes)
3 views6 pages

10 2DArrayPrograms

The document contains several Java programs demonstrating operations on 2D arrays, including displaying array elements, accepting user input, calculating sums of rows, columns, and diagonals. Each program is structured to handle a 3x3 or 4x4 matrix and utilizes nested loops for processing. The final program combines all functionalities to analyze a 3x3 matrix by displaying it and calculating the sums of rows, columns, and both diagonals.

Uploaded by

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

10 2DArrayPrograms

The document contains several Java programs demonstrating operations on 2D arrays, including displaying array elements, accepting user input, calculating sums of rows, columns, and diagonals. Each program is structured to handle a 3x3 or 4x4 matrix and utilizes nested loops for processing. The final program combines all functionalities to analyze a 3x3 matrix by displaying it and calculating the sums of rows, columns, and both diagonals.

Uploaded by

tanmaykedia2010
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAMS ON 2D Arrays

class ArrayDemo
{
public static void main()
{
int a[][] = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};

[Link]("Array Elements:");

for(int i = 0; i < [Link]; i++)


{
for(int j = 0; j < a[i].length; j++)
{
[Link](a[i][j] + "\t");
}
[Link]();
}
}
}
2. Accepting Data in a Double Dimensional Array
import [Link].*;

class AcceptArray
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];

[Link]("Enter 9 elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}

[Link]("Matrix:");

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


{
for(int j = 0; j < 3; j++)
{
[Link](a[i][j] + "\t");
}
[Link]();
}
}
}
3. Sum of Elements of Each Row
import [Link].*;

class RowSum
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];

[Link]("Enter elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}

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


{
int sum = 0;

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


{
sum += a[i][j];
}

[Link]("Sum of Row " + (i + 1) + " = " + sum);


}
}
}
4. Sum of Elements of Each Column
import [Link].*;

class ColumnSum
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];

[Link]("Enter elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}

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


{
int sum = 0;

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


{
sum += a[i][j];
}

[Link]("Sum of Column " + (j + 1) + " = " + sum);


}
}
}
5. Sum of Left Diagonal Elements (Principal Diagonal)

Elements where row index = column index

import [Link].*;

class LeftDiagonalSum
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];


int sum = 0;

[Link]("Enter elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}

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


{
sum += a[i][i];
}

[Link]("Sum of Left Diagonal = " + sum);


}
}
6. Sum of Right Diagonal Elements (Secondary Diagonal)

Condition:

i+j=n−1
import [Link].*;
class RightDiagonalSum
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];


int sum = 0;

[Link]("Enter elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}

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


{
sum += a[i][2 - i];
}

[Link]("Sum of Right Diagonal = " + sum);


}
}
7. Display Array in Matrix Format
import [Link].*;
class MatrixDisplay
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[4][4];

[Link]("Enter elements:");

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


{
for(int j = 0; j < 4; j++)
{
a[i][j] = [Link]();
}
}
[Link]("\nMatrix Format:");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
[Link]("%5d", a[i][j]);
}
[Link]();
}
}
}
8. Complete Program (Rows, Columns, Left & Right Diagonals Together)
import [Link].*;

class MatrixAnalysis
{
public static void main()
{
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];

[Link]("Enter 9 elements:");

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


{
for(int j = 0; j < 3; j++)
{
a[i][j] = [Link]();
}
}
[Link]("\nMatrix:");

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


{
for(int j = 0; j < 3; j++)
{
[Link](a[i][j] + "\t");
}
[Link]();
}
[Link]("\nRow Sums:");
for(int i = 0; i < 3; i++)
{
int sum = 0;
for(int j = 0; j < 3; j++)
{
sum += a[i][j];
}
[Link]("Row " + (i + 1) + " = " + sum);
}
[Link]("\nColumn Sums:");
for(int j = 0; j < 3; j++)
{
int sum = 0;
for(int i = 0; i < 3; i++)
{
sum += a[i][j];
}
[Link]("Column " + (j + 1) + " = " + sum);
}

int leftDiag = 0;
int rightDiag = 0;
for(int i = 0; i < 3; i++)
{
leftDiag += a[i][i];
rightDiag += a[i][2 - i];
}

[Link]("\nLeft Diagonal Sum = " + leftDiag);


[Link]("Right Diagonal Sum = " + rightDiag);
}
}

You might also like