Function printSumOfOddIntegers
Coding Problem Keys
Function printSumOfOddIntegers
Problem Statement
You must implement the function printSumOfOddIntegers which accepts an integer N and an integer matrix of size NxN as the input. The function must print the sum of odd integers in the matrix as the output.
Boundary Condition(s)
1 <= N <= 50.
1 <= Matrix element value <=1000.
Example Input/Output 1
Input
3
3 7 4
5 8 1
9 0 2
Output
25
Explanation
The odd integers in the matrix are 3, 7, 5, 1 and 9. So their sum is 25.
Hence the output is 25.
Example Input/Output 2
Input
4
10 25 10 35
31 80 21 50
50 75 30 45
11 40 91 60
Output
334
Note: Max Execution Time Limit: 2000 millisecs
Solution:
Programming Language: C Language
#include <stdio.h> #include <stdlib.h>
void printSumOfOddIntegers(int n,int matrix[][n]){ int sum=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(matrix[i][j]%2!=0){ sum=sum+matrix[i][j]; } } } printf("%d",sum); }
//Published By PKJCODERS
int main(){ int N; scanf("%d",&N); int matrix[N][N]; for(int row = 0;row < N; row++){ for(int col = 0; col < N; col++){ scanf("%d", &matrix[row][col]); } } printSumOfOddIntegers(N, matrix); return 0; }
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments