C program Roots or solution of Quadratic Equation
It has two C programs, they are
- function of quadratic equation -
A value x is input and f(x) is output of the program.
- solving a quadratic equation -
The coefficients a,b and c values are input and the solution value x1 and x2 are output of the program.
Function of x to Quadratic equation
The quadratic equation formed by three coefficients a,b and c are set constant in the program and a input x value is read from user to find f(x) (function of x).
y =f(x) = ax 2 + bx + c it is a quadratic function, find y =? of given x value Given : a=1.0,b=2.0,c=1.0 and x = 0.5 substitute the given into quadratic function. y = 1 * 0.5 2 + 2*0.5 + 1 = 1 * 0.25 + 1 +1 = 0.25 + 2 y= f(0.5) = 2.25
Function of x to Quadratic equation - C program
The C program finds function of x for given a input x. The input x c values is read from user while the program is running and then prints function of x as results of the program.
#include <stdio.h>
#include <string.h>
int main() {
const float a=1.0,b=2.0,c=1.0;
float x,fx;
printf ("\n Find f(x) of given x from quadratic equation");
printf("\n Enter x value :" );
scanf("%f", &x);
fx = a*x*x + b*x + c;
printf("\n Result ");
printf("\nx = %.2f ",x);
printf("\nf(x) = %.2f ",fx);
return 0;
}
Quadratic equation - C program Output
Find f(x) of given x from quadratic equation Enter x value 2.5 Result x =2.0 f(x) =9.00
Solution of quadratic equation
Quadratic equation
A quadratic equation is formed by 3 coefficients a,b and c. if we know values of these coefficients, we can solve it by finding roots of the equation.
Solution of quadratic equation
The solution x for a quadratic equation derived from this following formulae. The coefficients a,b and c values are substitute into this formula to obtain solution x.
if the sqrt has positive value, then solution x will be real number, otherwise the solution will be complex number.
Roots of Quadratic Equation - C program
The C program finds solution or roots of a given quadratic function. The coefficients a,b and c values is read from user while the program is running and then prints solution x as results of the program.
#include <stdio.h>
#include<math.h>
int main() {
float a,b,c;
char xtmp[50],x2tmp[50];
printf ("\n Find roots of the quadratic equation");
printf("\n Enter a value :" );
scanf("%f", &a);
printf("\n Enter b value :" );
scanf("%f", &b);
printf("\n Enter c value :" );
scanf("%f", &c);
float tmp =b*b - 4*a*c;
float tmp2 = 2.0*a;
if ( tmp>0 ) {
double x1 = (-b + sqrt(tmp) ) / tmp2;
double x2 = (-b - sqrt(tmp) ) / tmp2;
printf(" Root of the Equation is x1=%.2f x2=%.2f" ,x1,x2 );
}
else {
sprintf(xtmp,"%.2f+sqrt(%.2f)i/(2*%.2f)",-b,tmp,a);
sprintf(x2tmp,"%.2f-sqrt(%.2f)i/(2*%.2f)",-b,tmp,a);
printf("Root of the Equation is x= %s \n %s" , xtmp , x2tmp );
}
return 0;
}
Roots or solution of Quadratic Equation - Output
Find roots of the quadratic equation Enter a value 5 Enter b value 6 Enter c value 1 Root of the Equation is x1 = -0.20, x2=-1
Comments
Post a Comment