write a java program to find sum of all elements in a given matrix using array.

write a java program to find sum of all elements in a given matrix using array.


program:
import java.util.*;
public class matrixtrans
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the rows and columns of the matrix :");
int r=sc.nextInt();
int c=sc.nextInt();
int i, j,sum=0;
int a[ ][ ]=new int[r][c];
System.out.println("Enter the elements for matrix:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("elements of matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
sum+=a[i][j];
}
System.out.println();
}
System.out.println("sum of all elements in matrix:"+sum);
}
}


Output:
Enter the rows and columns of the matrix :
2
2
Enter the elements for matrix:
1
2
3
4
elements of matrix:
1       2
3       4
sum of all elements in matrix:10

Comments

Popular posts from this blog

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Write a java program to demonstrate static variable, methods, blocks.