This program will demonstrate how to solve a system of linear equations in two variables. We will use Cramer's rule for this.
Let us begin with the headers.
//WinterNurf Productions
#include <stdio.h>
#include <conio.h>
Next, we declare all the required variables after beginning the main() function.
intmain()
{
float A1, B1, C1;
float A2, B2, C2;
float x, y;
Next, we let the user know the format of the equations.
printf("A1(x) + B1(y) = C1\n");
printf("A2(x) + B2(y) = C2\n\n");
Now, we ask the user to enter all the required variables. We have used the scanf_s() function for this, but feel free to use scanf() instead.
printf("A1 = ");
scanf_s("%f", &A1);
printf("B1 = ");
scanf_s("%f", &B1);
printf("C1 = ");
scanf_s("%f", &C1);
printf("\nA2 = ");
scanf_s("%f", &A2);
printf("B2 = ");
scanf_s("%f", &B2);
printf("C2 = ");
scanf_s("%f", &C2);
If the equations do not have a unique solution, we print an error message.
if (((A1 * B2) - (B1 * A2)) == 0) {
printf("\nThe equatons do not have a unique soluton");
}
else {
x = ((C1 * B2) - (B1 * C2)) / ((A1 * B2) - (B1 * A2));
y = ((A1 * C2) - (C1 * A2)) / ((A1 * B2) - (B1 * A2));
printf("\nx = %f\n", x);
printf("y = %f", y);
}
Finally, we end the program.
_getch();
return 0;
}
Here is the full code.
//WinterNurf Productions
#include <stdio.h>
#include <conio.h>
intmain()
{
float A1, B1, C1;
float A2, B2, C2;
float x, y;
printf("A1(x) + B1(y) = C1\n");
printf("A2(x) + B2(y) = C2\n\n");
printf("A1 = ");
scanf_s("%f", &A1);
printf("B1 = ");
scanf_s("%f", &B1);
printf("C1 = ");
scanf_s("%f", &C1);
printf("\nA2 = ");
scanf_s("%f", &A2);
printf("B2 = ");
scanf_s("%f", &B2);
printf("C2 = ");
scanf_s("%f", &C2);
if (((A1 * B2) - (B1 * A2)) == 0) {
printf("\nThe equatons do not have a unique soluton");
}
else {
x = ((C1 * B2) - (B1 * C2)) / ((A1 * B2) - (B1 * A2));
y = ((A1 * C2) - (C1 * A2)) / ((A1 * B2) - (B1 * A2));
printf("\nx = %f\n", x);
printf("y = %f", y);
}
_getch();
return 0;
}This program has been written in Microsoft Visual Studio Professional 2013. It may not work in older compilers. However, simple editing will make the code compatible.