0% found this document useful (0 votes)
4 views7 pages

Java Array Element Sum Programs

The document contains six Java programs that perform various matrix operations. Program 1 calculates the sum of boundary elements, Program 2 sums the left diagonal elements, Program 3 sums the right diagonal elements, Program 4 sums both diagonals while accounting for even and odd matrix sizes, Program 5 sums each row's elements, and Program 6 sums each column's elements. Each program prompts the user for matrix dimensions and elements, then displays the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Array Element Sum Programs

The document contains six Java programs that perform various matrix operations. Program 1 calculates the sum of boundary elements, Program 2 sums the left diagonal elements, Program 3 sums the right diagonal elements, Program 4 sums both diagonals while accounting for even and odd matrix sizes, Program 5 sums each row's elements, and Program 6 sums each column's elements. Each program prompts the user for matrix dimensions and elements, then displays the results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 1

//sum of boundary elements of the array of size m x n


import [Link].*;
class BoundarySum{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m=[Link]();
[Link]("Enter Column Size...");
int n=[Link]();
int a[][]= new int[m][n];
int r, c, s=0;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
//boundary sum and display of the matrix
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
if(r==0||c==0||r==m-1||c==n-1)
s=s+a[r][c];
}
[Link]();
}
[Link]("Sum of boundary elements = "+s);
}
}
Program 2
//sum of elements at Left Diagonal of the array of size m x m
import [Link].*;
class SumOfLeftDiagonal{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m,n;
m = [Link]();
//column size is same as row
n = m;
int a[][]= new int[m][n];
int r, c, s=0;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
//Left Diagonal sum and display of the matrix
//Left Diagonal elements lies when r == c
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
if(r==c)
s=s+a[r][c];
}
[Link]();
}
[Link]("Sum of Left Diagonal Elements = "+s);
}
}
Program 3
//sum of elements at Right Diagonal of the array of size m x m
import [Link].*;
class SumOfRightDiagonal{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m,n;
m = [Link]();
//column size is same as row
n = m;
int a[][]= new int[m][n];
int r, c, s=0;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
//Right Diagonal sum and display of the matrix
//Right Diagonal elements lies when (r + c == m-1)
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
if((r + c) == (m - 1))
s=s+a[r][c];
}
[Link]();
}
[Link]("Sum of Right Diagonal Elements = "+s);
}
}
Program 4
//sum of elements at Left & Right Diagonal of the array of size m x m
import [Link].*;
class SumOfLeftAndRightDiagonal{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m,n;
m = [Link]();
//column size is same as row
n = m;
int a[][]= new int[m][n];
int r, c, s1=0,s2=0, s=0;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
if(m%2==0)
{
//Diagonals sum and display of the matrix
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
if((r + c) == (m - 1))
s1=s1+a[r][c];
if(r == c)
s2 = s2 + a[r][c];
}
[Link]();
}
s = s1 + s2;
[Link]("Sum of Right Diagonal Elements = "+s);
}
else
{
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
if((r + c) == (m - 1))
s1=s1+a[r][c];
if(r == c)
s2 = s2 + a[r][c];
}
[Link]();
}
s = s1 + s2 - a[m/2][n/2];
[Link]("Sum of Right Diagonal Elements = "+s);
}
}
}

Program 5
//sum of each Row elements of the array of size m x n
import [Link].*;
class RowSum{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m=[Link]();
[Link]("Enter Column Size...");
int n=[Link]();
int a[][]= new int[m][n];
int r, c, s;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
//row sum and display of the matrix
for (r = 0; r < m; r++)
{
s=0;
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
s=s+a[r][c];
}
[Link]("\t=\t"+s);
}
}
}

Program 6
//sum of each Column elements of the array of size m x m
import [Link].*;
class ColumnSum{
public static void main(){
Scanner sc = new Scanner([Link]);
[Link]("Enter Row Size...");
int m=[Link]();
int n = m;
int a[][]= new int[m][n];
int cs[]=new int[n];
int r, c, s,k=0;
for (r = 0; r < m; r++)
{
for (c = 0; c < n; c++)
{
[Link]("enter element...");
a[r][c] = [Link]();
}
}
//Column Sum and display of the matrix
for (r = 0; r < m; r++)
{
s=0;
for (c = 0; c < n; c++)
{
[Link](a[r][c]+"\t");
s=s+a[c][r];
}
cs[k++]=s;
[Link]();
}
[Link]("=========================");
for(c=0; c<n; c++)
[Link](cs[c]+"\t");
}
}

You might also like