This is a C Program to solve any linear equation in one variable.
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.
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.
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.
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.
#include <stdio.h>
#include <string.h>
float solve_for_y(float a, float b, float c)
{
float Y;
if(a == 0)
{
printf("Value of Y cannot be predicted\n");
}
else
{
Y = -(b + c) / a;
}
return Y;
}
int main()
{
float a, b, c, Y;
printf("\nEnter a linear equation in one variable of the form aY + b + c = 0 ");
printf("\nEnter the value of a, b, c respectively: ");
scanf("%f%f%f", &a, &b, &c);
Y = solve_for_y(a, b, c);
printf("\nSolution is Y = %f", Y);
return 0;
}
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.
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.
- Apply for C Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check C Books