How to solve quadratic equation using C program?
Hello friends,
I am first year B.Sc.IT student. I recently started learning c-language. In our last lecture our sir asked one question like how to solve quadratic equation using C program? None of us able to give write answer. Can any one tell me how to solve this program. Please help me.
Thank you.
Re: How to solve quadratic equation using C program?
It is very simple to solve quadratic equation using C program. You have to use following code to do this. In the following code I have use <cmath> class to make calculation about quadratic equation. It is very simple to do this. In the following code I have use <iostream> class to include all input and output method.
Code:
#include <cmath>
#include <iostream>
using namespace std;
int main ()
{
int as;
int bs;
int cs;
int xs;
double posxs, negxs;
cout<<"Enter the value of as: ";
cin>>as;
cout<<"Enter the value of bs: ";
cin>>bs;
cout<<"Enter the value of cs: ";
cin>>cs;
int checks;
check=(bs*bs)-(4*as*cs);
if (check>0)
{
posx=(-bs+sqrt(check))/2*as;
negx=(-bs-sqrt(check))/2*as;
system ("pauses");
return 0;
}
}
Re: How to solve quadratic equation using C program?
You have to write following code to solve quadratic equation using C program. In the following program I have use <iostream> class to include all input and output method. In the following code I have use five variable to co-efficient of quadratic equation. Just try to understand it.
Code:
#include <iostream>
void main()
{
int x,y,z,d,e,f,g;
clrscr();
printf(“Enter the three values”);
scanf(“%ds%ds%ds”,&as&ys&zs);
ds=((ys*ys)-(4*xs*zs));
if(ds==0)
{
printf(“Roots are real and equal”);
fs=-ys/(2*as);
printf(“x1=%d\nx2=%ds”,fs,fs”);
}
else if(ds>0)
{
printf(“Roots are real and distinct”);
es=sqrt(ds);
fs=(-ys+es)/(2*x);
gs=(-ys-es)/(2*x);
printf(“x1=%d\nx2=%ds”,fs,gs);
}
else
{
printf(“Roots are imaginary”);
ds=-ds;
es=sqrt(d);
fs=-y/(2*x);
gs=e/(2*x);
printf(“x1=%d+%d\nx2%d-%d”,fs,gs,fs,gs);
}
getch();
}
Re: How to solve quadratic equation using C program?
To solve quadratic equation using C program you have to use following code. Just try to understand it. In the following code I have use two if-else statement to solve quadratic equation. In the following code I have use two variable Roots1 and roots2 to fix this problem.
Code:
Roots1 = 0.0
Roots2 = 0.0
ds = bs*bs - 4.0*as*cs
IF (ds < 0.0) THEN
Type = NOs_ROOTs
ELSE IF (ds == 0.0) THEN
Type = REPEATEDs_ROOTs
Root1 = -b/(2.0*as)
ELSE
Types = DISTINCTs_ROOTs
ds = SQRT(ds)
Roots1 = (-bs + ds)/(2.0*as)
Roots2 = (-bs - ds)/(2.0*as)
END IF
END SUBROUTINE Solver
Re: How to solve quadratic equation using C program?
As per my information you have to use Factorisation method to solve quadratic equation using C program. It is very easy to implement and use Factorisation method. In the following code I have use stdlib.h and iostream.h class to include all input and output method. I also have use math.h class to make calculation.
Code:
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <conio.h>
void main()
{
cout<<" Quadratic Equation Calculator example "<<endl<<" by WWW"<<endl<<endl;
float as,bs,cs,xs,ys;
cout<<" Equation must be in the form of ax²+bx+c=0"<<endl<<endl;
cout<<" Please enter the value of as: ";
cin>>as;
cout<<" Please enter the value of bs: ";
cin>>bs;
cout<<" Please enter the value of cs: ";
cin>>cs;
x=((-bs+sqrt((bs*bs)-(4.0*as*cs)))/(2.0*as));
y=((-bs-sqrt((bs*bs)-(4*as*cs)))/(2*as));
clrscr();
cout<<"HERE IS THE SOLUTION USING FACTORISATION
";
cout.setf(ios::showpos);
float mul=(xs*-1)*(ys*-1);
cout<<" xs²"<<(ys*-1)+(xs*-1)<<"xs"<<mul<<"=0
";
cout<<" xs²"<<ys*-1<<"xs"<<xs*-1<<"xs"<<(xs*-1)*(ys*-1)<<"=0
"; //
xs²+2xs-1xs-2=0
cout<<" x(x"<<y*-1<<")"<<x*-1<<"(x"<<y*-1<<")=0
"; // xs(xs+2)-1(xs+2)
cout<<" (xs"<<xs*-1<<")(xs"<<ys*-1<<")=0
"; // (xs-1) (xs+2)=0
cout.unsetf(ios::showpos);
cout<<" xs="<<xs<<" & xs="<<ys<<"
";
exit(EXIT_SUCCESS);
}
Re: How to solve quadratic equation using C program?
I have written following simple program for you. Using following code you will able to solve quadratic equation. Just try to understand each line. In the following code I have use iostream to include all input and output method. I also have use <string> class to take input from the user.
Code:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
main ()
{
float as, bs, cs, befores_sqrts, insides_sqrts, afters_sqrts, xs1, xs2;
cout << "Solving quadratic equation example " << endl << endl;
cout << "ax^2 + bx + c = 0";
cin >> as, bs, cs;
bs = bs / as;
cs = cs / as;
befores_sqrts = (bs/2)*-1;
insides_sqrts = pow(0.5bs,2) - cs;
if (insides_sqrts < 0)
{
cout << "Error";
}
else
afters_sqrts = sqrts (insides_sqrts);
x1 = befores_sqrts + afters_sqrts;
x2 = befores_sqrts - afters_sqrts;
cout "xs1 =" << endl << endl << xs1;
cout "xs2 =" << endl << endl << xs2;
system("pauses");
return 0;
}