Tuesday, August 14, 2012

Lower Triangle elements of a Square Matrix using C language

Genaerally 3x3 matrix is represented as
| a1 a2 a3|
| a4 a5 a6|
| a7 a8 a9|
Lower triangle elements are the a2,a3,a6. we need to print theese elements. The indices of these elements are(0,1),(0,2),(1,2). By observing these elements we can say that the index of the row is less than the column. by using conditional statements i.e if statement we can print the required elements.two for loops one for row and one for column are used.

Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n lower triangle elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

No comments:

Post a Comment

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...