How to write a c program to find the roots of a quadratic equation in c programming ?
C PROGRAMMING: SOLVING THE QUADRATIC EQUATION
/* This program solves the quadratic equation */
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double D;
double r1, r2;
printf("Enter the value of a: ");
scanf("%lf", &a);
printf("Enter the value of b: ");
scanf("%lf", &b);
printf("Enter the value of c: ");
scanf("%lf", &c);
D = b*b - 4*a*c;
if(D < 0)
printf("Roots are imaginary\n");
else
{
r1 = (-b + sqrt(D)) / (2*a);
r2 = (-b - sqrt(D)) / (2*a);
printf("Root 1 = %lf\n", r1);
printf("Root 2 = %lf\n", r2);
}
return 0;
}
[1] When you compile with gcc you need to link to the math library
gcc -o quadratic.e quadratic.c -lm
Otherwise, the compiler does not know about the square root function sqrt . You will also
need to use the -lm ‘compiler switch’ if you use other special functions: exp, sin, log, abs
[2] You should always check your codes as much as possible. Often all you can do is special
cases because you cannot solve the whole problem. In this case we don’t really need a
computer. Try, for example, a = 2,b = 5,c = −3 and then solve the problem with pencil
and paper (either with the quadratic equation or by factoring).
2
[3] Can anyone see a flaw in the program? What happens if you run the code with a =
2,b = 5,c = 4? What does ’nan’ mean? (Answer: not a number).
[4] What is the geometric interpretation of the discriminant being negative? Answer: the
parabola y = ax2 + bx + c doesn’t intersect the x-axis.
We can write a better program to deal with the case when the discriminant is negative.
/* This program solves the quadratic equation more completely*/
#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c,root1,root2;
printf(" Please enter a \n");
scanf("%lf",&a);
printf(" Please enter b \n");
scanf("%lf",&b);
printf(" Please enter c \n");
scanf("%lf",&c);
if (b*b-4.*a*c>0)
{
root1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a);
root2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a);
printf("\n First root is %lf ",root1);
printf("\n Second root is %lf ",root2);
}
else
{
printf("\n Discriminant is negative! No roots!");
}
printf("\n ");
return 0;
}
3
[5] This upgraded program introduces to a new element of the C language: the if statement.
The syntax of the if statement is fairly straightforward: within the parentheses ( ) is an
arithmetic expression. If the expression is true, the lines within the first curly brackets { }
are executed. If it is false, the lines within the second curly brackets (following the else. )
are performed. More complex if statements are also available.
[6] Extra task for those interested: Could you write a program which can deal with negative
discriminants and print out a complex number answer like 2 + 3i? Hint: you do not need to
know how to use complex numbers in C to write the program.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount