Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


27. Write a program to evaluate the addition of diagonal elements of two square matrixes.


/*
 File: Prgrm27.c
 Author: Aditya Saini
 Date: Jan 18, 2021
 Description: Program to evaluate the addition of diagonal elements of two square matrixes.
*/

#include <stdio.h>

int main (void)
{
     int mat_1[20][20], mat_2[20][20];
     int size_1, size_2;
     int sum_1, sum_2;
     int i, j;

     //Input row and column of first matrix
     printf ("Input no of rows or columns of first matrix: ");
     scanf ("%d", &size_1);

     //Input first matrix
     printf ("\n***Input first matrix***\n");
     for (i = 0; i <= size_1 - 1; i++)
          for (j = 0; j <= size_1 - 1; j++)
          {
               printf ("Input element %d, %d: ", i + 1, j + 1);
               scanf ("%d", &mat_1[i][j]);
          }

     //Input row and column of second matrix
     printf ("\nInput no of rows or columns of second matrix: ");
     scanf ("%d", &size_2);

     //Input second matrix
     printf ("\n***Input second matrix***\n");
     for (i = 0; i <= size_2 - 2; i++)
          for (j = 0; j <= size_2 - 2; j++)
          {
               printf ("Input element %d, %d: ", i + 1, j + 1);
               scanf ("%d", &mat_2[i][j]);
          }

     //Print first matrix
     printf ("\n***First Matrix***\n");
     for (i = 0; i <= size_1 - 1; i++)
     {
          for (j = 0; j <= size_1 - 1; j++)
               printf ("%d\t", mat_1[i][j]);
          printf ("\n");
     }

     //Print second matrix
     printf ("\n***Second Matrix***\n");
     for (i = 0; i <= size_2 - 2; i++)
     {
          for (j = 0; j <= size_2 - 2; j++)
               printf ("%d\t", mat_2[i][j]);
          printf ("\n");
     }

     //Calculate sum of diagonal elements of first matrix
     sum_1 = 0;
     for (i = 0; i <= size_1 - 1; i++)
          sum_1 = sum_1 + mat_1[i][i];

     //Calculate sum of diagonal elements of second matrix
     sum_2 = 0;
     for (i = 0; i <= size_2 - 1; i++)
          sum_2 = sum_2 + mat_2[i][i];

     //Print result
     printf ("\nSum of diagonal elements of first matrix (Sum 1): %d", sum_1);
     printf ("\nSum of diagonal elements of second matrix: %d (Sum 2)\n", sum_2);

     if (sum_1 == sum_2)
          printf ("Sum 1 = sum 2");
     else if (sum_1 > sum_2)
          printf ("Sum 1 > Sum 2");
     else
          printf ("Sum 1 < Sum 2");

     return 0;
};

Output

Input no of rows or columns of first matrix: 3

***Input first matrix***
Input element 1, 1: 5
Input element 1, 2: 6
Input element 1, 3: 7
Input element 2, 1: 41
Input element 2, 2: 65
Input element 2, 3: 8
Input element 3, 1: 45
Input element 3, 2: 7
Input element 3, 3: 4

Input no of rows or columns of second matrix: 3

***Input second matrix***
Input element 1, 1: 14
Input element 1, 2: 78
Input element 1, 3: 45
Input element 2, 1: 78
Input element 2, 2: 57
Input element 2, 3: 43
Input element 3, 1: 19
Input element 3, 2: 46
Input element 3, 3: 35

***First Matrix***
5      6      7
41    65    8
45    7      4

***Second Matrix***
14    78    45
78    57    43
19    46    35

Sum of diagonal elements of first matrix (Sum 1): 74
Sum of diagonal elements of second matrix (Sum 2): 106
Sum 1 < Sum 2



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib