# sum of elements in the Zigzag sequence in a given matrix

Program to print the sum of elements in the Zigzag sequence in a given matrix is discussed here. Given a matrix, the sum of elements in the zigzag sequence is displayed as output.

## Algorithm to find the sum of elements in the zigzag sequence in a given matrix

• Input the order of the matrix.
• Input the matrix elements.
• Find the sum of elements of the matrix in zigzag sequence.
• Print the sum.

Program to find the sum of elements in the zigzag sequence in a given matrix is given below.

/* C Program to print the sum of elements in the Zigzag sequence in a given matrix */
#include
#include

int main()
{
int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;
scanf(“%d %d”,&m,&n);
int i, j;
int mat[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}

for(i = 0; i <= 0; i++)
{
for(j = 0; j < n-1; j++)
{
row1 = row1 + mat[i][j];
}
}

for(j = n – 1; j == n-1; j–)
{
for(i = 0; i < m; i++)
{
col_n = col_n + mat[j][i];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if ((i + j) == (m – 1))
{
diag += mat[i][j];
}
if(j == 0 && i == m-1)
col_n = col_n – mat[i][j];
}
}
printf(“Sum of Zig-Zag pattern is %d “, diag + row1 + col_n);
}

/* C++ Program to print the sum of elements in the Zigzag sequence in a given matrix */
#include<iostream>
using namespace std;
int main()
{
int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;
cin >> m >> n;
int i, j;
int mat[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat[i][j];
}

for(i = 0; i <= 0; i++)
{
for(j = 0; j < n-1; j++)
{
row1 = row1 + mat[i][j];
}
}

for(j = n – 1; j == n-1; j–)
{
for(i = 0; i < m; i++)
{
col_n = col_n + mat[j][i];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if ((i + j) == (m – 1))
{
diag += mat[i][j];
}
if(j == 0 && i == m-1)
col_n = col_n – mat[i][j];
}
}
cout << “Sum of Zig-Zag pattern is ” << diag + row1 + col_n;
}

/* Java Program to print the sum of elements in the Zigzag sequence in a given matrix */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int i, j;
int[][] mat = new int[m][n];
System.out.print(“\nInput the matrix elements \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat[i][j] = sc.nextInt();
}

for(i = 0; i <= 0; i++)
{
for(j = 0; j < n-1; j++)
{
row1 = row1 + mat[i][j];
}
}

for(j = n – 1; j == n-1; j–)
{
for(i = 0; i < m; i++)
{
col_n = col_n + mat[j][i];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if ((i + j) == (m – 1))
{
diag += mat[i][j];
}
if(j == 0 && i == m-1)
col_n = col_n – mat[i][j];
}
}
sum = diag + row1 + col_n;
System.out.print(“Sum of Zig-Zag pattern is ” + sum);

}

}

# Python Program to print the sum of elements in the Zigzag sequence in a given matrix

mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
row1 = 0
col_n = 0
diag = 0
m = 3
n = 3
for i in range(0):
for j in range(0, n-1):
row1 = row1 + mat[i][j]

for i in range(0,1):
for j in range(0, n-1):
row1 = row1 + mat[i][j]

for j in range(n-1, n-2, -1):
for i in range(0, m):
col_n = col_n + mat[j][i]

for i in range(0, m):
for j in range(0, n):
if ((i + j) == (m – 1)):
diag = diag + mat[i][j]
if(j == 0 and i == m-1):
col_n = col_n – mat[i][j]

sum = diag + row1 + col_n;
print(“Sum of Zig-Zag pattern is “, sum)

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,i,j,r,c;
scanf(“%d”,&r);
scanf(“%d”,&c);

a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
*(a+i)=(int*)malloc(c*sizeof(int));

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,*(a+i)+j);
}
}

i=0;int sum1=0;
for(j=0;j<c-1;j++)
sum1=sum1+*(*(a+i)+j);

i=r-1;int sum3=0;
for(j=1;j<c;j++)
sum3=sum3+*(*(a+i)+j);

i=0;j=c-1;int sum2=0;
while(i<r&&j>=0)
{
sum2=sum2+*(*(a+i)+j);
i++;
j–;
}
printf(“Sum of Zig-Zag pattern is %d”,sum1+sum2+sum3);
return 0;
}

Output: