C Program to Find Solution of Quadratic Equation
Given a quadratic equation in the form ax2 + bx + c, find roots of it.
Attention reader! Don't stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
Examples :
Input : a = 1, b = -2, c = 1 Output : Roots are real and same 1 Input : a = 1, b = 7, c = 12 Output : Roots are real and different -3, -4 Input : a = 1, b = 1, c = 1 Output : Roots are complex -0.5 + i1.73205 -0.5 - i1.73205
Below is the direct formula for finding roots of the quadratic equation.
There are the following important cases.
If b*b < 4*a*c, then roots are complex (not real). For example roots of x2 + x + 1, roots are -0.5 + i1.73205 and -0.5 - i1.73205 If b*b == 4*a*c, then roots are real and both roots are same. For example, roots of x2 - 2x + 1 are 1 and 1 If b*b > 4*a*c, then roots are real and different. For example, roots of x2 - 7x - 12 are 3 and 4
Below is the implementation of the above formula.
C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void
findRoots(
int
a,
int
b,
int
c)
{
if
(a == 0) {
printf
(
"Invalid"
);
return
;
}
int
d = b * b - 4 * a * c;
double
sqrt_val =
sqrt
(
abs
(d));
if
(d > 0) {
printf
(
"Roots are real and different \n"
);
printf
(
"%f\n%f"
, (
double
)(-b + sqrt_val) / (2 * a),
(
double
)(-b - sqrt_val) / (2 * a));
}
else
if
(d == 0) {
printf
(
"Roots are real and same \n"
);
printf
(
"%f"
, -(
double
)b / (2 * a));
}
else
{
printf
(
"Roots are complex \n"
);
printf
(
"%f + i%f\n%f - i%f"
, -(
double
)b / (2 * a),
sqrt_val/(2 * a), -(
double
)b / (2 * a), sqrt_val/(2 * a);
}
}
int
main()
{
int
a = 1, b = -7, c = 12;
findRoots(a, b, c);
return
0;
}
C++
#include <bits/stdc++.h>
using
namespace
std;
void
findRoots(
int
a,
int
b,
int
c)
{
if
(a == 0) {
cout <<
"Invalid"
;
return
;
}
int
d = b * b - 4 * a * c;
double
sqrt_val =
sqrt
(
abs
(d));
if
(d > 0) {
cout <<
"Roots are real and different \n"
;
cout << (
double
)(-b + sqrt_val) / (2 * a) <<
"\n"
<< (
double
)(-b - sqrt_val) / (2 * a);
}
else
if
(d == 0) {
cout <<
"Roots are real and same \n"
;
cout << -(
double
)b / (2 * a);
}
else
{
cout <<
"Roots are complex \n"
;
cout << -(
double
)b / (2 * a) <<
" + i"
<< sqrt_val
<<
"\n"
<< -(
double
)b / (2 * a) <<
" - i"
<< sqrt_val;
}
}
int
main()
{
int
a = 1, b = -7, c = 12;
findRoots(a, b, c);
return
0;
}
Java
import
java.io.*;
import
static
java.lang.Math.*;
class
Quadratic {
static
void
findRoots(
int
a,
int
b,
int
c)
{
if
(a ==
0
) {
System.out.println(
"Invalid"
);
return
;
}
int
d = b * b -
4
* a * c;
double
sqrt_val = sqrt(abs(d));
if
(d >
0
) {
System.out.println(
"Roots are real and different \n"
);
System.out.println(
(
double
)(-b + sqrt_val) / (
2
* a) +
"\n"
+ (
double
)(-b - sqrt_val) / (
2
* a));
}
else
if
(d ==
0
) {
System.out.println(
"Roots are real and same \n"
);
System.out.println(-(
double
)b / (
2
* a) +
"\n"
+ -(
double
)b / (
2
* a));
}
else
{
System.out.println(
"Roots are complex \n"
);
System.out.println(-(
double
)b / (
2
* a) +
" + i"
+ sqrt_val +
"\n"
+ -(
double
)b / (
2
* a)
+
" - i"
+ sqrt_val);
}
}
public
static
void
main(String args[])
{
int
a =
1
, b = -
7
, c =
12
;
findRoots(a, b, c);
}
}
Python3
import
math
def
findRoots(a, b, c):
if
a
=
=
0
:
print
(
"Invalid"
)
return
-
1
d
=
b
*
b
-
4
*
a
*
c
sqrt_val
=
math.sqrt(
abs
(d))
if
d >
0
:
print
(
"Roots are real and different "
)
print
((
-
b
+
sqrt_val)
/
(
2
*
a))
print
((
-
b
-
sqrt_val)
/
(
2
*
a))
elif
d
=
=
0
:
print
(
"Roots are real and same"
)
print
(
-
b
/
(
2
*
a))
else
:
print
(
"Roots are complex"
)
print
(
-
b
/
(
2
*
a),
" + i"
, sqrt_val)
print
(
-
b
/
(
2
*
a),
" - i"
, sqrt_val)
a
=
1
b
=
-
7
c
=
12
findRoots(a, b, c)
C#
using
System;
class
Quadratic {
void
findRoots(
int
a,
int
b,
int
c)
{
if
(a == 0) {
Console.Write(
"Invalid"
);
return
;
}
int
d = b * b - 4 * a * c;
double
sqrt_val = Math.Abs(d);
if
(d > 0) {
Console.Write(
"Roots are real and different \n"
);
Console.Write(
(
double
)(-b + sqrt_val) / (2 * a) +
"\n"
+ (
double
)(-b - sqrt_val) / (2 * a));
}
else
{
Console.Write(
"Roots are complex \n"
);
Console.Write(-(
double
)b / (2 * a) +
" + i"
+ sqrt_val +
"\n"
+ -(
double
)b / (2 * a) +
" - i"
+ sqrt_val);
}
}
public
static
void
Main()
{
Quadratic obj =
new
Quadratic();
int
a = 1, b = -7, c = 12;
obj.findRoots(a, b, c);
}
}
PHP
<?php
function
findRoots(
$a
,
$b
,
$c
)
{
if
(
$a
== 0)
{
echo
"Invalid"
;
return
;
}
$d
=
$b
*
$b
- 4 *
$a
*
$c
;
$sqrt_val
= sqrt(
abs
(
$d
));
if
(
$d
> 0)
{
echo
"Roots are real and "
.
"different \n"
;
echo
(-
$b
+
$sqrt_val
) / (2 *
$a
) ,
"\n"
,
(-
$b
-
$sqrt_val
) / (2 *
$a
);
}
else
if
(
$d
== 0)
{
echo
"Roots are real and same \n"
;
echo
-
$b
/ (2 *
$a
);
}
else
{
echo
"Roots are complex \n"
;
echo
-
$b
/ (2 *
$a
) ,
" + i"
,
$sqrt_val
,
"\n"
, -
$b
/ (2 *
$a
),
" - i"
,
$sqrt_val
;
}
}
$a
= 1;
$b
= -7 ;
$c
= 12;
findRoots(
$a
,
$b
,
$c
);
?>
Javascript
<script>
function
findRoots(a, b, c)
{
if
(a == 0) {
document.write(
"Invalid"
);
return
;
}
let d = b * b - 4 * a * c;
let sqrt_val = Math.sqrt(Math.abs(d));
if
(d > 0) {
document.write(
"Roots are real and different \n"
+
"<br/>"
);
document.write(
(-b + sqrt_val) / (2 * a) +
"<br/>"
+ (-b - sqrt_val) / (2 * a));
}
else
if
(d == 0) {
document.write(
"Roots are real and same \n"
+
"<br/>"
);
document.write(-b / (2 * a) +
"<br/>"
+ -b / (2 * a)) ;
}
else
{
document.write(
"Roots are complex \n"
);
document.write(-b / (2 * a) +
" + i"
+ sqrt_val +
"<br/>"
+ -b / (2 * a)
+
" - i"
+ sqrt_val);
}
}
let a = 1, b = -7, c = 12;
findRoots(a, b, c);
</script>
Output
Roots are real and different 4.000000 3.000000
This article is contributed by Dheeraj Gupta. Please write comments if you find anything incorrect, or hare more information about the topic discussed above.
C Program to Find Solution of Quadratic Equation
Source: https://www.geeksforgeeks.org/program-to-find-the-roots-of-quadratic-equation/