Write a program to find the sum of boundary elements of the matrix.
Example:
Input: matrix[][3]={{1,2,3},{4,5,6},{7,8,9}}
Output: Sum of boundary elements is 40
C Program
#include <stdio.h>int main(){int n = 3;int matrix[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};int sum = 0;for (int i = 0; i < n; i++){//sum of first row and last rowif (i == 0 || i == n - 1){for (int j = 0; j < n; j++){sum = sum + matrix[i][j];}}else{//first elementsum = sum + matrix[i][0];//last elementsum = sum + matrix[i][n - 1];}}printf("Sum of boundary elements is %d", sum);return 0;}
No comments:
Post a Comment