1. ## Efficient Coding??

Ok the question is this..........

Write a function that will calculate and display the real roots of the quadratic equation
ax2 + bx+c = 0
use the function in the program.

I have coded this
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
int a, b, c, D;
double root1, root2;

printf("Enter a,b,c for the quadratic equation\n");
scanf("%d %d %d",&a,&b,&c);

check(a, b, c, D, root1, root2);
}
int check(int a, int b, int c, int D, double root1, double root2)
{
if ((a==0) && (b==0)) {
printf("This equation has no solution");
}

else if (a==0){
root1=(-c/a);
printf("This equation has only one root %lf",root1);
}

else {
D = (b*b - 4*a*c);
if (D>0){
root1 = (-b + sqrt(D))/(2*a);
root2 = (-b - sqrt(D))/(2*a);
printf("The real roots are %lf & %lf",root1,root2);
}
else if (D<0){
printf("There are no real roots");
}
else {
root1 = root2 = (-b/2*a*c);
printf("The repeated roots are %lf",root1);
}
}
}```
This is working fine, but i want to know that is it efficient & correct answer to the question??
if not how to make it more efficient.
Also how can i make it multi function code.......i mean check will be for checking of a, b, c & then it will return to main function & new calculate function will be called where all calculations will be made

2. first of all your code need a proper indendation

Code:
```#include <stdio.h>
#include <math.h>

int check(int, int, int);

int main(void)
{
int a, b, c;

printf("Enter a,b,c for the quadratic equation\n");
scanf("%d %d %d",&a,&b,&c);

check(a, b, c);

getchar();
return 0;
}

int check(int a, int b, int c)
{
int D;

if ((a==0) && (b==0))
printf("This equation has no solution");
else if (a==0)
printf("This equation has only one root %lf",(-c/a));
// your code is broken here. Anything divided by 0 is ???
else
{
D = (b*b - 4*a*c);
if (D>0)
printf("The real roots are %lf & %lf",(-b + sqrt(D))/(2*a*c),(-b - sqrt(D))/(2*a*c));
else if (D<0)
printf("There are no real roots");
else
printf("The repeated roots are %lf",(-b/2*a*c));
}
}```
ssharish2005

3. You got the divisor wrong. It should be:
Code:
```        if (D>0)
printf("The real roots are %lf & %lf",(-b + sqrt(D))/(2*a),(-b - sqrt(D))/(2*a));```
as he had it. But otherwise, nice job.

4. and
Code:
`printf("The repeated roots are %lf",(-b/2*a));`
ssharish2005

5. Originally Posted by ssharish2005
and
Code:
`printf("The repeated roots are %lf",(-b/2*a));`
ssharish2005
or
-b/2.0/a
or
-b/(2.0*a)

6. True, never looked at that

ssharish2005