Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a matrix and calculate the row-wise and column-wise sums.

Easy

Problem Statement

Create a matrix and calculate the row-wise and column-wise sums.

Sample Input

Matrix: 
 [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6

Sample Output

Row-wise sums: 3 7 11 
Column-wise sums: 9 12

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
# Predefined structure matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE) row_sums <- rowSums(matrix) col_sums <- colSums(matrix) print(paste("Row-wise sums:", paste(row_sums, collapse = " "))) print(paste("Column-wise sums:", paste(col_sums, collapse = " ")))
Log in or sign up to run or submit your code.Log inSign up
Input
Matrix: 
 [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Expected Output
Row-wise sums: 3 7 11 
Column-wise sums: 9 12