0% found this document useful (0 votes)
7 views3 pages

Array 2d

The Java program takes a matrix of integers as input and checks for invalid conditions such as negative numbers or dimensions outside the range of 3 to 10. It calculates the sum of prime numbers in each row and column, identifying the row and column with the maximum prime sum. The program then outputs the indices and sums of these rows and columns with the highest prime sums.

Uploaded by

Arpita Singh
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)
7 views3 pages

Array 2d

The Java program takes a matrix of integers as input and checks for invalid conditions such as negative numbers or dimensions outside the range of 3 to 10. It calculates the sum of prime numbers in each row and column, identifying the row and column with the maximum prime sum. The program then outputs the indices and sums of these rows and columns with the highest prime sums.

Uploaded by

Arpita Singh
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

import [Link].

*;
class shreya
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter order:");
int M=[Link]();
int N=[Link]();
int A[][]=new int[M][N];
int i,k,j,max=0,m=0,r=0,c=0,f=0,ff=0;
if(M<3||M>10||N<3||N>10)
{
[Link]("invalid order");
}
else
{
[Link]("Enter elements of matrix:");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
A[i][j]=[Link]();
if (A[i][j]<0)
{ ff=1;break;
}
}
}
if (ff==1)
{ [Link]("INVALID INPUT"); }
else
{
[Link]("Original matrix:");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
[Link](A[i][j]+" ");
}[Link]();
}
int row[]=new int[M]; int col[]=new int[N];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
for(k=1;k<=A[i][j];k++)
{
if(A[i][j]%k==0)
f++;
}
if(f==2)
row[i]+=A[i][j];
f=0;
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
for(k=1;k<=A[j][i];k++)
{
if(A[j][i]%k==0)
f++;
}
if(f==2)
col[i]+=A[j][i];
f=0;
}
}
for(i=0;i<M;i++)
{
if(row[i]>m)
{ m=row[i]; r=i;}
}
for(i=0;i<N;i++)
{
if(col[i]>max)
{ max=col[i]; c=i;}
}
int p1=0,q1=0;
[Link]("Row with max prime-sum: "+r+"(sum="+m+")");
[Link]("Column with max prime-sum: "+c+"(sum="+max+")");
}
}
}
}

You might also like