Write a C program that computes the solutions for x and y in the linear system of equations

2.2k Views Asked by At

what's wrong with my code that i can't get the values for x and y, assume that the denominator is not zero. i can't get constant values of x and y, most of the time its equal to zero. a1x + b1y = c1 a2x + b2y = c2

#include <stdio.h>
#include <math.h>
int main()
{
 /* Write your program code here */
 int a1,b1,c1,a2,b2,c2,x,y;
 printf("Enter the value for a1: \n");
 scanf("%d", &a1);
 printf("Enter the value for b1: \n");
 scanf("%d", &b1);
 printf("Enter the value for c1: \n");
 scanf("%d", &c1);
 printf("Enter the value for a2: \n");
 scanf("%d", &a2);
 printf("Enter the value for b2: \n");
 scanf("%d", &b2);
 printf("Enter the value for c2: \n");
 scanf("%d", &c2);
 x=((b2*c1)-(b1*c2))/((a1*b2)-(a2*b1));
 printf("x is %d\n", x);
 y=((a1*c2)-(a2*c1))/((a1*b2)-(a2*b1));
 printf("y is %d\n", y);
 return 0;
}
1

There are 1 best solutions below

2
Joe Vanjik On

Maybe you need to change the type for x and y variables from int to float or double if you want accurate/precise results for x and y.

Using int type will not show you same results like float/double type because it won't show you numbers after decimal point instead it's truncated towards zero, yielding largest whole number which is smaller that floating point number for numbers greater than zero, and smallest whole number which is larger than floating number for numbers below zero.