Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
27. Write a program to find the sum of diagonal elements of a m x n matrix.
/*
File: Prgrm27.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to find the sum of diagonal elements of a m x n matrix.
*/
#include <stdio.h>
int main (void)
{
int matrix [10][10];
int i, j, row, column, sum;
//Input matrix.
printf ("***Input matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row);
printf ("Input number of columns: ");
scanf ("%d", &column);
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix [i][j]);
}
}
//Output matrix.
printf ("\n***Matrix***\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("%d\t", matrix [i][j]);
}
printf ("\n");
}
//Calculate and output the sum of diagonal elements.
for (i = 0, sum = 0; i < row && i < column; i++)
{
sum = sum + matrix [i][i];
}
printf ("\nSum of Diagonal elelments: %d", sum);
return 0;
};
/*
File: Prgrm27.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to find the sum of diagonal elements of a m x n matrix.
*/
#include <stdio.h>
int main (void)
{
int matrix [10][10];
int i, j, row, column, sum;
//Input matrix.
printf ("***Input matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row);
printf ("Input number of columns: ");
scanf ("%d", &column);
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix [i][j]);
}
}
//Output matrix.
printf ("\n***Matrix***\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("%d\t", matrix [i][j]);
}
printf ("\n");
}
//Calculate and output the sum of diagonal elements.
for (i = 0, sum = 0; i < row && i < column; i++)
{
sum = sum + matrix [i][i];
}
printf ("\nSum of Diagonal elelments: %d", sum);
return 0;
int i, j, row, column, sum;
//Input matrix.
printf ("***Input matrix***\n");
printf ("Input number of rows: ");
scanf ("%d", &row);
printf ("Input number of columns: ");
scanf ("%d", &column);
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("Input element %d %d: ", i + 1, j + 1);
scanf ("%d", &matrix [i][j]);
}
}
//Output matrix.
printf ("\n***Matrix***\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("%d\t", matrix [i][j]);
}
printf ("\n");
}
//Calculate and output the sum of diagonal elements.
for (i = 0, sum = 0; i < row && i < column; i++)
{
sum = sum + matrix [i][i];
}
printf ("\nSum of Diagonal elelments: %d", sum);
return 0;
};
Output
***Input matrix***
Input number of rows: 3
Input number of columns: 4
Input element 1 1: 5
Input element 1 2: 9
Input element 1 3: 7
Input element 1 4: 11
Input element 2 1: 4
Input element 2 2: 8
Input element 2 3: 6
Input element 2 4: 10
Input element 3 1: 3
Input element 3 2: 1
Input element 3 3: 2
Input element 3 4: 12
***Matrix***
5 9 7 11
4 8 6 10
3 1 2 12
Sum of Diagonal elements: 15
***Input matrix***
Input number of rows: 3
Input number of columns: 4
Input element 1 1: 5
Input element 1 2: 9
Input element 1 3: 7
Input element 1 4: 11
Input element 1 4: 11
Input element 2 1: 4
Input element 2 2: 8
Input element 2 3: 6
Input element 2 4: 10
Input element 2 4: 10
Input element 3 1: 3
Input element 3 2: 1
Input element 3 3: 2
Input element 3 4: 12
Input element 3 4: 12
***Matrix***
5 9 7 11
4 8 6 10
3 1 2 12
Sum of Diagonal elements: 15
No comments:
Post a Comment
Please do not post spam links.