Given a 3×3 matrix the task is to calculate the sum of odd elements in matrix.
Example: Consider the 3×3 matrix
| 4 | 1 | 8 |
| 3 | 7 | 9 |
| 2 | 1 | 6 |
Sum of all elements = 21
Java
import java.util.*;
class SumMatrixOE
{
public static void main(String args[])
{
int sum=0;
Scanner sc=new Scanner(System.in);
int ar[][]=new int[3][3];
System.out.println("Enter matrix elements:-");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
ar[i][j]=sc.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(ar[i][j]%2!=0)
{
sum=sum+ar[i][j];
}
}
}
System.out.println("Sum of odd elements in matrix: "+sum);
}
}Java