Hello all,
I want to rewrite the quadratic equation as a function and as a function in the main. Below are the two programs but was not giving accourate answer.
For the function alone:
Code:
float quadratic(float x1, float x2)
{
float a, b, c, d, g, h=0.0;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
d=sqrt((b*b)-4*a*c);
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x1=(-b+d/g);
x2=(-b-d/g);
printf ("\nFirst real root is," x1);
printf ("\nSecond real root is," x2);
}
return (x1, x2);
}
it was not printing any values neither showing any calculations done
The second one that has main with it
Code:
/* program to calculate the root of a quadratic equation*/
#include <stdio.h>
#include <math.h>
float quadratic(float a, float b, float c)
{
float d, g, h=0.0, x1;
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x1=(-b+((b*b)-4*a*c))/g;
}
return (x1);
}
float quadratic1(float a, float b, float c)
{
float d, g, h=0.0, x2;
g=2*a;
if (g==h)
{
printf ("\nYou have performed an illegal operation as the divisor cannot be zero.");
}
else
{
x2=(-b-((b*b)-4*a*c))/g;
}
return (x2);
}
main()
{
float a, b, c, x1, x2;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
x1 = quadratic(a,b,c);
x2 = quadratic1(a,b,c);
printf("\na=%f,", a);
printf("\nb=%f,", b);
printf("\nc=%f,", c);
printf("\nThe real roots are %f, %f", x1, x2);
}
Please help me out.