Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program Eliminasi Gauss

#include<iostream>
#define MAX 10
using namespace std;
int lcm(int x,int y);
int main()
{
int i,j,k,r,c,a[MAX][MAX],b[MAX][MAX],det=1,l,d1,d2;
cout<<"\nEnter the number of Rows of the Matrix: ";
cin>>r;
cout<<"\nEnter the number of Columns of the Matrix: ";
cin>>c;
if(r==c)
{
cout<<"\nEnter the Elements of the Matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<r-1;i++)
{
for(j=i+1;j<r;j++)
{
l=lcm(a[i][i],a[j][i]);
if(l!=0&&(a[i][i]!=0&&a[j][i]!=0))
{
l=(a[i][i]*a[j][i])/l;
d1=l/a[i][i];
d2=l/a[j][i];
a[j][i]=0;
for(k=i+1;k<r;k++)
{
a[j][k]=(d2*a[j][k])-(d1*a[i][k]);
}
}
}
}
cout<<"\nThe given Matrix after Gauss Elimination is:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
else
{
cout<<"\nThis is not a Square Matrix!!!\n";
}
return 0;
}
int lcm(int x,int y)

{
int t;
while (y != 0)
{
t=y;
y=x%y;
x=t;
}
return x;
}

You might also like