# Solving Linear Equations in One Variable Using C

This is a C Program to solve any linear equation in one variable.

Problem Description

For linear equation of the form aY + b + c = 0, we need to input value of a,b,c. After having values of all the constants we need to solve for Y and create a function which will return the calculated value of Y.

Expected Input and Output

Case 1. When the coefficient of Y is zero.

```If a = 0, then we cannot predict the value of Y
because the product "a*Y" in the equation will become 0.```

Case 2. When all the constants are positive:
For example:

```If the value of a = 1, b = 1 and c = 1
then Y = -2.```

Case 3. When constants are both negative and positive:
For example:

```If the value of a = 1, b = -2 and c = -1
then Y = 3.```
Problem Solution

1. Input the values of a,b,c.
2. Put them in the given equation and make the resulting equation equal to 0.
3. Solve for Y.

Program/Source Code

Here is source code of solving any linear equation in one variable. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.

1. `#include <stdio.h>`
2. `#include <string.h>`
3. `float solve_for_y(float a, float b, float c)`
4. `{`
5. `     float Y;`
6. `     if(a == 0)`
7. `     {`
8. `          printf("Value of Y cannot be predicted\n");`
9. `     }`
10. `     else`
11. `     {`
12. `          Y = -(b + c) / a;`
13. `     }`
14. `    return Y;`
15. `}`
16. `int main()`
17. `{`
18. `  float a, b, c, Y;`
19. `  printf("\nEnter a linear equation in one variable of the form aY + b + c = 0 ");`
20. `  printf("\nEnter the value of a, b, c respectively: ");`
21. `  scanf("%f%f%f", &a, &b, &c);`
22. `  Y = solve_for_y(a, b, c);`
23. `  printf("\nSolution is Y = %f", Y);`
24. `  return 0;`
25. `}`
Program Explanation

1. Here in this program we have taken 3 variables a, b and c where a is the coefficient of Y.
2. We have to solve for Y. It can simply evaluated as -(b+c)/a.
3. Since value of Y can have fractional values that is why we have taken its data type as float.

Runtime Test Cases
```1. Enter a linear equation in one variable of the form aY + b + c = 0
Enter the value of a, b, c respectively: 0 1 1
Value of Y cannot be predicted.
2. Enter a linear equation in one variable of the form aY + b + c = 0
Enter the value of a, b, c respectively: 1 1 1

Solution is Y = -2.000000
3. Enter a linear equation in one variable of the form aY + b + c = 0
Enter the value of a, b, c respectively: 1 -2 -1

Solution is Y = 3.000000```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.