Knowee
Questions
Features
Study Tools

Aryan is an enthusiastic young programmer who loves exploring matrices. Aryan is currently working on a program to calculate the sum of all even elements in a square matrix. He needs your help to complete the code.Write a program to assist Aryan in achieving his goal. The program should take an integer N as input, representing the size of a square matrix. Then, the program should read the elements of the matrix and calculate the sum of all even elements. Finally, it should print the total sum.Input format :The first line consists of a single integer N, representing the size of the square matrix.The next N lines contain N space-separated integers, representing the elements of the square matrix.Output format :If there are even elements in the matrix, display the sum of all even elements.If there are no even elements in the matrix, display "0".Refer to the sample output for the formatting specifications.Code constraints :

Question

Aryan is an enthusiastic young programmer who loves exploring matrices. Aryan is currently working on a program to calculate the sum of all even elements in a square matrix. He needs your help to complete the code.Write a program to assist Aryan in achieving his goal. The program should take an integer N as input, representing the size of a square matrix. Then, the program should read the elements of the matrix and calculate the sum of all even elements. Finally, it should print the total sum.Input format :The first line consists of a single integer N, representing the size of the square matrix.The next N lines contain N space-separated integers, representing the elements of the square matrix.Output format :If there are even elements in the matrix, display the sum of all even elements.If there are no even elements in the matrix, display "0".Refer to the sample output for the formatting specifications.Code constraints :

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that fulfills the requirements:

# Function to calculate sum of even elements in a matrix
def sum_of_even_elements(matrix, n):
    sum = 0
    for i in range(n):
        for j in range(n):
            if matrix[i][j] % 2 == 0:
                sum += matrix[i][j]
    return sum

# Input size of matrix
n = int(input())

# Initialize matrix
matrix = []

# Input elements of matrix
for i in range(n):
    row = list(map(int, input().split()))
    matrix.append(row)

# Calculate and print sum of even elements
sum = sum_of_even_elements(matrix, n)
print(sum)

This program first takes an integer input n for the size of the square matrix. It then takes n lines of input, each containing n space-separated integers, to form the matrix. The function sum_of_even_elements is used to calculate the sum of all even elements in the matrix. This sum is then printed. If there are no even elements in the matrix, the sum will be 0, so "0" will be printed.

This problem has been solved

Similar Questions

Let A be a square matrix of size n x n. Consider the following program. What is the expected output? #include <stdio.h>int main() {    int A[2][2]={1,2,3,4},i,j,C=100,Temp,n=2;    for(i=0;i<n;i++)        for(j=0;j<n;j++){            Temp = A[i][j] + C;            A[i][j] = A[j][i];            A[j][i] = Temp - C;        }    for(i=0;i<n;i++){        for(j=0;j<n;j++)            printf("%d ",A[i][j]);        printf("\n");    }    return 0;}

Write a program to obtain a matrix and find the sum of the elements in the lower triangular matrix.Note: Only square matrixInput format :The first line of the input consists of the number of rows and columns.The second line of the input consists of the matrix element.Output format :The output prints the sum of the lower triangular matrix.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 312 23 4556 78 8995 51 20Output 1 :312

Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151

Problem StatementHarpreet is a student learning about matrix multiplication. She wants to write a program to multiply two matrices, a and b, each of size N x N. Can you help her with the code?Write a program that takes an integer N as input and two matrices a and b of size N x N. Implement matrix multiplication and print the resulting matrix c.Note: A square matrix is where the number of rows equals the number of columns.Input format :The first line of input consists of an integer N, representing the matrix size.The next N lines consist of N space-separated elements in each line, representing the first matrix.After being separated by a new line, the next N lines consist of N space-separated elements in each line, representing the second matrix.Output format :The output prints the product of two matrices.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:2 ≤ N ≤ 80 ≤ elements ≤ 9Sample test cases :Input 1 :32 3 23 2 33 3 34 5 62 3 11 2 3Output 1 :16 23 21 19 27 29 21 30 30 Input 2 :22 22 35 07 8Output 2 :24 16 31 24 Input 3 :81 5 2 4 7 3 2 12 3 4 5 6 7 8 94 5 6 1 2 3 7 82 5 8 7 4 1 9 61 4 7 8 5 2 6 99 8 6 4 7 5 1 27 5 3 9 5 1 4 83 4 8 9 7 1 2 05 8 7 4 6 1 2 09 7 4 5 6 3 2 17 5 6 8 4 1 2 31 4 5 2 3 9 8 72 5 8 7 4 3 6 94 5 8 7 1 0 0 11 5 6 7 8 4 2 23 2 1 4 3 5 3 6Output 3 :99 131 152 141 106 88 97 115 145 200 236 248 183 155 137 181 155 177 179 207 172 108 88 113 157 201 217 233 202 166 146 169 149 186 206 223 179 176 157 194 204 243 255 231 185 110 128 136 152 208 212 197 188 177 164 179 136 178 206 184 146 133 148 159

Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.Input3 31 2 3 4 5 6 7 8 9OutputSum of the main diagonal elements: 15Sum of the anti-diagonal elements: 15

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.