C program to find the solution of linear equation

CServer Side ProgrammingProgramming

We can apply the software development method to solve the linear equation of one variable in C programming language.

Requirement

  • The equation should be in the form of ax+b=0
  • a and b are inputs, we need to find the value of x

Analysis

Here,

  • An input is the a,b values.
  • An output is the x value.

Algorithm

Refer an algorithm given below to find solution of linear equation.

Step 1. Start
Step 2. Read a,b values
Step 3. Call function
Jump to step 5
Step 4. Print result
Step 5:
  • i. if(a == 0)
    • Print value of c cannot be predicted
  • Else
    • Compute c=-b/a
  • Return c
Step 6: Stop

Program

Following is the C program to find the solution of linear equation −

 Live Demo

#include <stdio.h>
#include <string.h>
float solve(float a, float b){
   float c;
   if(a == 0){
      printf("value of c cannot be predicted\n");
   }else{
      c = -b / a;
   }
   return c;
}
int main(){
   float a, b, c;
   printf("\n enter a,b values: ");
   scanf("%f%f", &a, &b);
   c = solve(a, b);
   printf("\n linear eq of one variable in the form of ax+b = 0, if a=%f,b=%f,then x=    %f",a,b,c);
   return 0;
}

Output

When the above program is executed, it produces the following result −

enter a,b values: 4 8
linear eq of one variable in the form of ax+b = 0, if a=4.000000, b=8.000000,
then x= -2.000000
raja
Published on 26-Mar-2021 07:04:06
Advertisements