0

I want to find the solutions of linear equation of two variable(i know that there are infinitly many solution but i have to print some of them!)

so i tried in C language what i have did is simply taken the input of coefficient of X,Y and c. Then calucalted the values as you can see it in the code Then calculated that if the result is satisfying the equation(means it is =0 or not) then checked and print. After that i have incremented the value of x,y so that it can be calculaed with different numbers(in the first i have put it by default 0)..

here is the code please see the code and tell me what is problem and am i doing mistake to choose the type of variable etc.. if possible please give me the correct code(request).

code in C:-

#include <stdio.h>

int main()
{
    float a, b, c, Rx, Ry;
    int sol;

    float x, y;
    printf("The Linear equation in two variable\n");
    printf("Formate is   aX+bY+c=0\n");
    printf("Enter the coefficient of 'X'\n");
    scanf("%f", &a);
    printf("Enter the coefficient of 'Y'\n");
    scanf("%f", &b);
    printf("Enter the coefficient of constant 'c'\n");
    scanf("%f", &c);

    printf("Your equation looks like\n");
    printf("%0.0fX+%0.0fY+%0.0f=0\n", a, b, c);

    x = 0;
    y = 0;
    for (int i = 0; i < 10; i++)
    {
        //  for x

        x = ((-c) + (-b * y)) / b;
        printf("The value of x is %0.2f\n", x);

        // for y

        y = ((-c) + (-a * x)) / a;
        printf("The value of y is %0.2f\n", y);

        // checking wheter the value of x and y satisfy the equation (which is equal to 0)
        sol = (a * x) + (b * y) + (c);
        printf("The value of sol is %0.2f\n", sol);

        x++;
        y++;
        if (sol == 0)
        {
            printf("The value of x at %d time is %0.2f\n", i, x);
            printf("The value of y at %d time is %0.2f\n", i, y);
        }
        else
            continue;
    }

    return 0;
}
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39

1 Answers1

0

am i doing mistake to choose the type of variable

   int sol;
   …
       printf("The value of sol is %0.2f\n", sol);

Whether the choice of type int for sol is a mistake depends on which precision you want. In any case, the conversion specifier f and type int don't fit together; you have to bring those in line. Be aware that if you choose float for sol, the comparison sol == 0 will not very likely succeed.

Armali
  • 18,255
  • 14
  • 57
  • 171