Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #define ERROR 0.001
- int main()
- {
- int n,i,j,key,flag;
- float a[10][10],b[10],sum,x[10],x0[10],s=0;
- printf("enter the no of equations ");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- printf("enter coefficients of equation %d\t",i+1);
- for(j=0;j<n;j++)
- scanf("%f",&a[i][j]);
- printf("\nenter constant of equation %d\t",i+1);
- scanf("%f",&b[i]);
- }
- for(i=0;i<n;i++)
- {
- s=0;
- for(j=0;j<n;j++)
- {
- if(i!=j)
- s+=fabs(a[i][j]);
- }
- if(fabs(a[i][i])<s)
- {
- printf("error");
- exit(1);
- }
- }
- for(i=0;i<n;i++)
- x0[i]=b[i]/a[i][i];
- flag=1;
- while(flag)
- {
- key=0;
- for(i=0;i<n;i++)
- {
- sum=b[i];
- for(j=0;j<n;j++)
- {
- if(j!=i)
- {
- sum=sum-(a[i][j]*x0[j]);
- }
- }
- x[i]=sum/a[i][i];
- if(key==0)
- if(fabs((x[i]-x0[i])/x[i])>ERROR)
- key=1;
- }
- if(key==1)
- {
- for(i=0;i<n;i++)
- {
- x0[i]=x[i];
- }
- }
- else
- flag=0;
- }
- printf("the solutions for the variables are");
- for(i=0;i<n;i++)
- {
- printf("%f\t",x[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment