About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to find addition of Lower Triangular Elements in C Programming

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,a[10][10],sum,m,n;

/* m - Number of rows 
   n - Number of Columns */

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

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

/* Accept the Elements in m x n Matrix */

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
       printf("Enter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

/* Addition of all Diagonal Elements */

sum = 0;

for( i = 0 ; i < m ; i++ )
       for( j = 0 ; j < n ; j++ )
       {
        if ( i > j )             // Condition for Lower Triangle
        sum = sum + a[i][j];
       }

/* Print out the Result */

printf("nThe Addition of Lower Triangle Elements : %d",sum);

getch();

}

Output :
Enter the number of Rows : 3

Enter the number of Columns : 3

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1

The Addition of Lower Triangle Elements : 5
Explanation : 

Considering above 3×3 matrix  -
  1. By Observing , it is clear that when i > j Condition is true then and then only we have to add the elements

No comments: