Slip 8 - B) Write a menu driven program to perform the following operation on m*n Matrix 1. Calculate sum of upper triangular matrix elements 2. Calculate sum of diagonal elements

Solution:


#include <stdio.h>

void main()
{
    int fig_code;
    float side, base, length, breadth, height, area, radius;

    printf("-------------------------\n");
    printf(" 1 --> sum of upper\n");
    printf(" 2 --> sum of dignola\n");
    printf("-------------------------\n");
    int i, j, a[10][10], sum, rows, columns;

   printf("\nEnter the number of Rows : ");
   scanf("%d", &rows);

   printf("\nEnter the number of Columns : ");
   scanf("%d", &columns);

   //Accept the Elements in Matrix
   for (i = 0; i < rows; i++)
      for (j = 0; j < columns; j++) {
         printf("\nEnter the Element a[%d][%d] : ", i, j);
         scanf("%d", &a[i][j]);
      }


    printf("Enter the Figure code\n");

    scanf("%d", &fig_code);
    switch(fig_code)
    {
 
    case 1:
        sum = 0;
        for (i = 0; i < rows; i++)
      for (j = 0; j < columns; j++) {
         // Condition for Upper Triangle
         if (i < j) {
            sum = sum + a[i][j];
         }
      }
printf("sum of upper=%d",sum);
        break;
    case 2:
sum = 0;
   for (i = 0; i < rows; i++)
      for (j = 0; j < columns; j++) {
         // Condition for Upper Triangle
         if (i == j) {
            sum = sum + a[i][j];
         }
      }
printf("sum of digonal=%d",sum);
        break;
 
    default:
        printf("Error in figure code\n");
        break;
    }
}

Post a Comment

0 Comments