Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A simple program to solve quadratic equations with

4.00/5 (7 votes)
8 Nov 2010CPOL 39.9K  
Simple and prints imaginary roots too!float a,b,c,x1,x2,d,dsq;printf(ax^2 + bx + c = 0);printf(nEnter a,b,c separated by commas : n);scanf(%f,%f,%f,&a,&b,&c);d = b*b-(4*a*c);if(d>=0){dsq=sqrt(d);x1 = (-b+dsq)/(2*a);x2 = (-b-(dsq))/(2*a);printf(nRoot 1 : %fnRoot 2...
Simple and prints imaginary roots too!
C#
float a,b,c,x1,x2,d,dsq;
printf("ax^2 + bx + c = 0");
printf("\nEnter a,b,c separated by commas : \n");
scanf("%f,%f,%f",&a,&b,&c);
d = b*b-(4*a*c);
if(d>=0)
{
dsq=sqrt(d);
x1 = (-b+dsq)/(2*a);
x2 = (-b-(dsq))/(2*a);
printf("\nRoot 1 : %f\nRoot 2 : %f",x1,x2);
}
if(d<0)
{
d = ((4*a*c)-pow(b,2))/(2*a);
printf("\nRoot 1 : %f+%fi",((-b)/(2*a)),d);
printf("\nRoot 2 : %f-%fi",((-b)/(2*a)),d);}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)