Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<math.h>
- #define error 0.0001
- void main()
- {
- double a[50][50],b[50],y[50],z[50],s=0,sum;
- int f,k=1,n,i,j,key=0;
- printf("enter the number of equations to enter\n");
- scanf("%d",&n);
- do
- {
- f=1;
- for(i=0;i<n;i++)
- {
- printf("enter the coefficients of equation:%d\n",i+1);
- for(j=0;j<n;j++)
- scanf("%lf",&a[i][j]);
- scanf("%lf",&b[i]);
- for(j=0;j<(n-1);j++)
- s=s+a[i][j];
- s=s-a[i][i];
- if(fabs(a[i][i])<fabs(s))
- {
- printf("the equation doesnt converge re-enter equations\n");
- f=0;
- break;
- }
- }
- }while(f==0);
- for(i=0;i<n;i++)
- y[i]=(b[i]/a[i][i]);
- do
- {
- k=1;
- key=0;
- for(i=0;i<n;i++)
- {
- sum=b[i];
- for(j=0;j<n;j++)
- {
- if(j==i)
- break;
- sum=sum-(a[i][j]*y[j]);
- }
- z[i]=(sum/a[i][i]);
- if(fabs((z[i]-y[i])/z[i])>error)
- key=1;
- }
- if(key==1)
- {
- for(i=0;i<n;i++)
- y[i]=z[i];
- k=0;
- }
- }while(k==0);
- printf("the values of the unknowns are:\n");
- for(i=0;i<n;i++)
- printf("x %d %.4lf\n",i+1,z[i]);
- }
- OUTPUT:
- enter the number of equations to enter
- 3
- enter the coefficients of equation:1
- 2 1 1 5
- enter the coefficients of equation:2
- 3 5 2 15
- enter the coefficients of equation:3
- 2 1 4 8
- the values of the unknowns are:
- x 1 2.5000
- x 2 1.5000
- x 3 0.3750
- Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment