write a C program to accept a 2D array from the user and find the sum of all even elements and product of all the odd elements in the 2D array.
Answers
Answer:
#include <stdio.h>
int main()
{
int rows , collumns , evenSum = 0 , oddProduct = 1;
printf("Enter the number of rows in the matrix:");
scanf("%d" , &rows);
printf("Enter the number of collumns in the matrix:");
scanf("%d" , &collumns);
int arr[rows][collumns];
printf("Enter all the elements in the matrx- \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < collumns; j++){
scanf("%d" , &arr[i][j]);
}
}
printf("---THE MATRIX YOU ENTERED--- \n");
for(int i = 0; i < rows; i++){
for(int j = 0; j < collumns; j++){
printf("%d " , arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < collumns; j++){
if(arr[i][j] % 2 == 0){
evenSum = evenSum + arr[i][j];
}
else if(arr[i][j] % 2 == 1){
oddProduct = oddProduct * arr[i][j];
}
}
}
printf("The sum of all even numbers int the matrix is %d \n" , evenSum);
printf("The product of all odd numbers int the matrix is %d \n" , oddProduct);
return 0;
}
Explanation: