Find square root of quadratic equation
The given problem involves finding the square roots of a quadratic equation in the form ax^2 + bx + c = 0, where 'a', 'b', and 'c' are the coefficients of the quadratic equation and 'x' represents the unknown variable.
Problem Statement
The task is to write a program that calculates the square roots of a given quadratic equation and displays the results. The program should handle three different cases based on the discriminant of the equation.
Explanation with Suitable Example
Let's consider the quadratic equation: 4x^2 + 8x + 4 = 0
Here, 'a' is 4, 'b' is 8, and 'c' is 4.
Standard Pseudocode
1. Start
2. Read coefficients a, b, and c
3. Calculate discriminant (D) = b^2 - 4ac
4. If D == 0, then
4.1 Calculate the single root r = -b / 2a
4.2 Print "The quadratic equation has a single real root: r"
5. Else if D > 0, then
5.1 Calculate two distinct real roots r1 = (-b + sqrt(D)) / 2a and r2 = (-b - sqrt(D)) / 2a
5.2 Print "The quadratic equation has two distinct real roots: r1 and r2"
6. Else (D < 0), then
6.1 Calculate real part r = -b / 2a and imaginary part i = sqrt(-D) / 2a
6.2 Print "The quadratic equation has two distinct complex roots: r + i and r - i"
7. End
Algorithm with Proper Explanation
-
Start the program and define the
squareRoot
function that takes three arguments 'a', 'b', and 'c'. -
Calculate the discriminant (D) using the formula
discriminant = (b * b) - (4 * a * c)
. -
Based on the value of the discriminant, follow these conditions:
- If the discriminant is zero, calculate the single root using
r1 = (-b) / (2 * a)
and setr2
to the same value. This indicates two identical real roots. - If the discriminant is positive, calculate two distinct real roots using the formulas
r1 = ((-b) + sqrt(discriminant)) / (2 * a)
andr2 = ((-b) - sqrt(discriminant)) / (2 * a)
. - If the discriminant is negative, calculate the real and imaginary parts separately. The real part is
r = (-b) / (2 * a)
and the imaginary part isimaginary = sqrt(-discriminant) / (2 * a)
.
- If the discriminant is zero, calculate the single root using
-
Print the results for each case accordingly.
-
In the
main
function, call thesquareRoot
function for three different quadratic equations.
Code Solution
Here given code implementation process.
// C program
// Find square root of quadratic equation
#include <stdio.h>
#include <math.h>
void squareRoot(float a, float b, float c)
{
// Quadratic formula
/* _______
−b ± √b2−4ac
x = ————————————
2a
*/
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
float discriminant = (b * b) - (4 * a * c);
float r1 = 0.0;
float r2 = 0.0;
// Display parameter
printf("\n Given a : %f",a);
printf("\n Given b : %f",b);
printf("\n Given c : %f",c);
if(discriminant == 0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 * a);
r2 = r1;
// Print two equal and real roots
printf("\n X = %f ",r1);
printf("\n X = %f ",r2);
}
else
{
if(discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + sqrt(discriminant)) / (2 * a);
r2 = ((-b) - sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
printf("\n X = %f ",r1);
printf("\n X = %f ",r2);
}
else
{
// (b²−4ac) < 0
float imaginary = sqrt(-discriminant) / (2 * a);
r1 = (-b) / (2 * a);
r2 = r1;
// Print two distinct complex roots
printf("\n X = %f + %fi ",r1, imaginary);
printf("\n X = %f - %fi ",r2, imaginary);
}
}
printf("\n");
}
int main(int argc, char
const *argv[])
{
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
squareRoot( 7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
squareRoot(5, 7, 2);
return 0;
}
Output
Given a : 4.000000
Given b : 8.000000
Given c : 4.000000
X = -1.000000
X = -1.000000
Given a : 7.000000
Given b : 3.000000
Given c : 2.000000
X = -0.214286 + 0.489690i
X = -0.214286 - 0.489690i
Given a : 5.000000
Given b : 7.000000
Given c : 2.000000
X = -0.400000
X = -1.000000
/*
Java program
Find square root of quadratic equation
*/
public class QuadraticEquation
{
public void squareRoot(double a, double b, double c)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
double discriminant = (b * b) - (4 * a * c);
double r1 = 0.0;
double r2 = 0.0;
// Display parameter
System.out.print("\n Given a : " + a );
System.out.print("\n Given b : " + b );
System.out.print("\n Given c : " + c );
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 * a);
r2 = r1;
// Print two equal and real roots
System.out.print("\n X = " + r1 );
System.out.print("\n X = " + r2 );
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + Math.sqrt(discriminant)) / (2 * a);
r2 = ((-b) - Math.sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
System.out.print("\n X = " + r1 );
System.out.print("\n X = " + r2 );
}
else
{
// (b²−4ac) < 0
double imaginary = Math.sqrt(-discriminant) / (2 * a);
r1 = (-b) / (2 * a);
r2 = r1;
// Print two distinct complex roots
System.out.print("\n X = " + r1 + " + " + imaginary + "i ");
System.out.print("\n X = " + r2 + " - " + imaginary + "i ");
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
QuadraticEquation task = new QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot( 7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
}
}
Output
Given a : 4.0
Given b : 8.0
Given c : 4.0
X = -1.0
X = -1.0
Given a : 7.0
Given b : 3.0
Given c : 2.0
X = -0.21428571428571427 + 0.48968961431436026i
X = -0.21428571428571427 - 0.48968961431436026i
Given a : 5.0
Given b : 7.0
Given c : 2.0
X = -0.4
X = -1.0
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
/*
C++ program
Find square root of quadratic equation
*/
class QuadraticEquation
{
public: void squareRoot(double a, double b, double c)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
double discriminant = (b *b) - (4 *a *c);
double r1 = 0.0;
double r2 = 0.0;
// Display parameter
cout << "\n Given a : " << a;
cout << "\n Given b : " << b;
cout << "\n Given c : " << c;
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 *a);
r2 = r1;
// Print two equal and real roots
cout << "\n X = " << r1;
cout << "\n X = " << r2;
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + sqrt(discriminant)) / (2 *a);
r2 = ((-b) - sqrt(discriminant)) / (2 *a);
// Print two distinct and real roots
cout << "\n X = " << r1;
cout << "\n X = " << r2;
}
else
{
// (b²−4ac) < 0
double imaginary = sqrt(-discriminant) / (2 *a);
r1 = (-b) / (2 *a);
r2 = r1;
// Print two distinct complex roots
cout << "\n X = " << r1 << " + " << imaginary << "i ";
cout << "\n X = " << r2 << " - " << imaginary << "i ";
}
}
cout << "\n";
}
};
int main()
{
QuadraticEquation task = QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
return 0;
}
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.214286 + 0.48969i
X = -0.214286 - 0.48969i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Find square root of quadratic equation
*/
public class QuadraticEquation
{
public void squareRoot(double a, double b, double c)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
double discriminant = (b * b) - (4 * a * c);
double r1 = 0.0;
double r2 = 0.0;
// Display parameter
Console.Write("\n Given a : " + a);
Console.Write("\n Given b : " + b);
Console.Write("\n Given c : " + c);
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 * a);
r2 = r1;
// Print two equal and real roots
Console.Write("\n X = " + r1);
Console.Write("\n X = " + r2);
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + Math.Sqrt(discriminant)) / (2 * a);
r2 = ((-b) - Math.Sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
Console.Write("\n X = " + r1);
Console.Write("\n X = " + r2);
}
else
{
// (b²−4ac) < 0
double imaginary = Math.Sqrt(-discriminant) / (2 * a);
r1 = (-b) / (2 * a);
r2 = r1;
// Print two distinct complex roots
Console.Write("\n X = " + r1 + " + " + imaginary + "i ");
Console.Write("\n X = " + r2 + " - " + imaginary + "i ");
}
}
Console.Write("\n");
}
public static void Main(String[] args)
{
QuadraticEquation task = new QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
}
}
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.214285714285714 + 0.48968961431436i
X = -0.214285714285714 - 0.48968961431436i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1
<?php
/*
Php program
Find square root of quadratic equation
*/
class QuadraticEquation
{
public function squareRoot($a, $b, $c)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
$discriminant = ($b * $b) - (4 * $a * $c);
$r1 = 0.0;
$r2 = 0.0;
// Display parameter
echo "\n Given a : ". $a;
echo "\n Given b : ". $b;
echo "\n Given c : ". $c;
if ($discriminant == 0)
{
// When (b²−4ac) == 0
$r1 = ((-$b) / (2 * $a));
$r2 = $r1;
// Print two equal and real roots
echo "\n X = ". $r1;
echo "\n X = ". $r2;
}
else
{
if ($discriminant > 0)
{
// When (b²−4ac) > 0
$r1 = (((-$b) + sqrt($discriminant)) / (2 * $a));
$r2 = (((-$b) - sqrt($discriminant)) / (2 * $a));
// Print two distinct and real roots
echo "\n X = ". $r1;
echo "\n X = ". $r2;
}
else
{
// (b²−4ac) < 0
$imaginary = (sqrt(-$discriminant) / (2 * $a));
$r1 = ((-$b) / (2 * $a));
$r2 = $r1;
// Print two distinct complex roots
echo "\n X = ". $r1 ." + ". $imaginary ."i ";
echo "\n X = ". $r2 ." - ". $imaginary ."i ";
}
}
echo "\n";
}
}
function main()
{
$task = new QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
$task->squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
$task->squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
$task->squareRoot(5, 7, 2);
}
main();
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.21428571428571 + 0.48968961431436i
X = -0.21428571428571 - 0.48968961431436i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1
/*
Node Js program
Find square root of quadratic equation
*/
class QuadraticEquation
{
squareRoot(a, b, c)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
var discriminant = (b * b) - (4 * a * c);
var r1 = 0.0;
var r2 = 0.0;
// Display parameter
process.stdout.write("\n Given a : " + a);
process.stdout.write("\n Given b : " + b);
process.stdout.write("\n Given c : " + c);
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = ((-b) / (2 * a));
r2 = r1;
// Print two equal and real roots
process.stdout.write("\n X = " + r1);
process.stdout.write("\n X = " + r2);
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + Math.sqrt(discriminant)) / (2 * a);
r2 = ((-b) - Math.sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
process.stdout.write("\n X = " + r1);
process.stdout.write("\n X = " + r2);
}
else
{
// (b²−4ac) < 0
var imaginary = (Math.sqrt(-discriminant) / (2 * a));
r1 = ((-b) / (2 * a));
r2 = r1;
// Print two distinct complex roots
process.stdout.write("\n X = " + r1 + " + " + imaginary + "i ");
process.stdout.write("\n X = " + r2 + " - " + imaginary + "i ");
}
}
process.stdout.write("\n");
}
}
function main()
{
var task = new QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
}
main();
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.21428571428571427 + 0.48968961431436026i
X = -0.21428571428571427 - 0.48968961431436026i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1
# Ruby program
# Find square root of quadratic equation
class QuadraticEquation
def squareRoot(a, b, c)
# Quadratic formula
# _______
# −b ± √b2−4ac
# x = ————————————
# 2a
#
# Calculate polynomial with an order of 2
# ax²+bx+c = 0
discriminant = (b * b) - (4 * a * c)
r1 = 0.0
r2 = 0.0
# Display parameter
print("\n Given a : ", a)
print("\n Given b : ", b)
print("\n Given c : ", c)
if (discriminant == 0)
# When (b²−4ac) == 0
r1 = (-b) / (2 * a)
r2 = r1
# Print two equal and real roots
print("\n X = ", r1)
print("\n X = ", r2)
else
if (discriminant > 0)
# When (b²−4ac) > 0
r1 = ((-b) + Math.sqrt(discriminant)) / (2 * a)
r2 = ((-b) - Math.sqrt(discriminant)) / (2 * a)
# Print two distinct and real roots
print("\n X = ", r1)
print("\n X = ", r2)
else
# (b²−4ac) < 0
imaginary = Math.sqrt(-discriminant) / (2 * a)
r1 = (-b).to_f / (2 * a)
r2 = r1
# Print two distinct complex roots
print("\n X = ", r1 ," + ", imaginary ,"i ")
print("\n X = ", r2 ," - ", imaginary ,"i ")
end
end
print("\n")
end
end
def main()
task = QuadraticEquation.new()
# Case A
# 4x²+8x+4 = 0
# a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4)
# Case B
# 7x²+3x+2 = 0
# a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2)
# Case C
# 5x²+7x+2 = 0
# a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2)
end
main()
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.21428571428571427 + 0.48968961431436026i
X = -0.21428571428571427 - 0.48968961431436026i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1.0
/*
Scala program
Find square root of quadratic equation
*/
class QuadraticEquation
{
def squareRoot(a: Double, b: Double, c: Double): Unit = {
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
var discriminant: Double = (b * b) - (4 * a * c);
var r1: Double = 0.0;
var r2: Double = 0.0;
// Display parameter
print("\n Given a : " + a);
print("\n Given b : " + b);
print("\n Given c : " + c);
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = ((-b) / (2 * a));
r2 = r1;
// Print two equal and real roots
print("\n X = " + r1);
print("\n X = " + r2);
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = (((-b) + Math.sqrt(discriminant)) / (2 * a));
r2 = (((-b) - Math.sqrt(discriminant)) / (2 * a));
// Print two distinct and real roots
print("\n X = " + r1);
print("\n X = " + r2);
}
else
{
// (b²−4ac) < 0
var imaginary: Double = (Math.sqrt(-discriminant) / (2 * a));
r1 = ((-b) / (2 * a));
r2 = r1;
// Print two distinct complex roots
print("\n X = " + r1 + " + " + imaginary + "i ");
print("\n X = " + r2 + " - " + imaginary + "i ");
}
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: QuadraticEquation = new QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
}
}
Output
Given a : 4.0
Given b : 8.0
Given c : 4.0
X = -1.0
X = -1.0
Given a : 7.0
Given b : 3.0
Given c : 2.0
X = -0.21428571428571427 + 0.48968961431436026i
X = -0.21428571428571427 - 0.48968961431436026i
Given a : 5.0
Given b : 7.0
Given c : 2.0
X = -0.4
X = -1.0
import Foundation
/*
Swift 4 program
Find square root of quadratic equation
*/
class QuadraticEquation
{
func squareRoot(_ a: Double, _ b: Double, _ c: Double)
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
let discriminant: Double = (b * b) - (4 * a * c);
var r1: Double = 0.0;
var r2: Double = 0.0;
// Display parameter
print("\n Given a : ", a, terminator: "");
print("\n Given b : ", b, terminator: "");
print("\n Given c : ", c, terminator: "");
if (discriminant == 0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 * a);
r2 = r1;
// Print two equal and real roots
print("\n X = ", r1, terminator: "");
print("\n X = ", r2, terminator: "");
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + sqrt(discriminant)) / (2 * a);
r2 = ((-b) - sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
print("\n X = ", r1, terminator: "");
print("\n X = ", r2, terminator: "");
}
else
{
// (b²−4ac) < 0
let imaginary: Double = sqrt(-discriminant) / (2 * a);
r1 = (-b) / (2 * a);
r2 = r1;
// Print two distinct complex roots
print("\n X = ", r1 ," + ", imaginary ,"i ", terminator: "");
print("\n X = ", r2 ," - ", imaginary ,"i ", terminator: "");
}
}
print(terminator: "\n");
}
}
func main()
{
let task: QuadraticEquation = QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2);
}
main();
Output
Given a : 4.0
Given b : 8.0
Given c : 4.0
X = -1.0
X = -1.0
Given a : 7.0
Given b : 3.0
Given c : 2.0
X = -0.214285714285714 + 0.48968961431436 i
X = -0.214285714285714 - 0.48968961431436 i
Given a : 5.0
Given b : 7.0
Given c : 2.0
X = -0.4
X = -1.0
/*
Kotlin program
Find square root of quadratic equation
*/
class QuadraticEquation
{
fun squareRoot(a: Double, b: Double, c: Double): Unit
{
// Quadratic formula
// _______
// −b ± √b2−4ac
// x = ————————————
// 2a
//
// Calculate polynomial with an order of 2
// ax²+bx+c = 0
var discriminant: Double = (b * b) - (4 * a * c);
var r1: Double ;
var r2: Double ;
// Display parameter
print("\n Given a : " + a);
print("\n Given b : " + b);
print("\n Given c : " + c);
if (discriminant == 0.0)
{
// When (b²−4ac) == 0
r1 = (-b) / (2 * a);
r2 = r1;
// Print two equal and real roots
print("\n X = " + r1);
print("\n X = " + r2);
}
else
{
if (discriminant > 0)
{
// When (b²−4ac) > 0
r1 = ((-b) + Math.sqrt(discriminant)) / (2 * a);
r2 = ((-b) - Math.sqrt(discriminant)) / (2 * a);
// Print two distinct and real roots
print("\n X = " + r1);
print("\n X = " + r2);
}
else
{
// (b²−4ac) < 0
var imaginary: Double = Math.sqrt(-discriminant) / (2 * a);
r1 = (-b) / (2 * a);
r2 = r1;
// Print two distinct complex roots
print("\n X = " + r1 + " + " + imaginary + "i ");
print("\n X = " + r2 + " - " + imaginary + "i ");
}
}
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: QuadraticEquation = QuadraticEquation();
// Case A
// 4x²+8x+4 = 0
// a = 4, b = 8, c = 4
task.squareRoot(4.0, 8.0, 4.0);
// Case B
// 7x²+3x+2 = 0
// a = 7, b = 3, c= 2
task.squareRoot(7.0, 3.0, 2.0);
// Case C
// 5x²+7x+2 = 0
// a = 5, b = 7, c = 2
task.squareRoot(5.0, 7.0, 2.0);
}
Output
Given a : 4.0
Given b : 8.0
Given c : 4.0
X = -1.0
X = -1.0
Given a : 7.0
Given b : 3.0
Given c : 2.0
X = -0.21428571428571427 + 0.48968961431436026i
X = -0.21428571428571427 - 0.48968961431436026i
Given a : 5.0
Given b : 7.0
Given c : 2.0
X = -0.4
X = -1.0
import math
# Python 3 program
# Find square root of quadratic equation
class QuadraticEquation :
def squareRoot(self, a, b, c) :
# Quadratic formula
# _______
# −b ± √b2−4ac
# x = ————————————
# 2a
#
# Calculate polynomial with an order of 2
# ax²+bx+c = 0
discriminant = (b * b) - (4 * a * c)
r1 = 0.0
r2 = 0.0
# Display parameter
print("\n Given a : ", a, end = "")
print("\n Given b : ", b, end = "")
print("\n Given c : ", c, end = "")
if (discriminant == 0) :
# When (b²−4ac) == 0
r1 = int((-b) / (2 * a))
r2 = r1
# Print two equal and real roots
print("\n X = ", r1, end = "")
print("\n X = ", r2, end = "")
else :
if (discriminant > 0) :
# When (b²−4ac) > 0
r1 = (((-b) + math.sqrt(discriminant)) / (2 * a))
r2 = (((-b) - math.sqrt(discriminant)) / (2 * a))
# Print two distinct and real roots
print("\n X = ", r1, end = "")
print("\n X = ", r2, end = "")
else :
# (b²−4ac) < 0
imaginary = (math.sqrt(-discriminant) / (2 * a))
r1 = ((-b) / (2 * a))
r2 = r1
# Print two distinct complex roots
print("\n X = ", r1 ," + ", imaginary ,"i ", end = "")
print("\n X = ", r2 ," - ", imaginary ,"i ", end = "")
print(end = "\n")
def main() :
task = QuadraticEquation()
# Case A
# 4x²+8x+4 = 0
# a = 4, b = 8, c = 4
task.squareRoot(4, 8, 4)
# Case B
# 7x²+3x+2 = 0
# a = 7, b = 3, c= 2
task.squareRoot(7, 3, 2)
# Case C
# 5x²+7x+2 = 0
# a = 5, b = 7, c = 2
task.squareRoot(5, 7, 2)
if __name__ == "__main__": main()
Output
Given a : 4
Given b : 8
Given c : 4
X = -1
X = -1
Given a : 7
Given b : 3
Given c : 2
X = -0.21428571428571427 + 0.48968961431436026 i
X = -0.21428571428571427 - 0.48968961431436026 i
Given a : 5
Given b : 7
Given c : 2
X = -0.4
X = -1.0
Resultant Output Explanation
Let's go through the output obtained from the given code:
-
For the quadratic equation 4x^2 + 8x + 4 = 0, the output shows:
Given a: 4.000000 Given b: 8.000000 Given c: 4.000000 X = -1.000000 X = -1.000000
This indicates that the quadratic equation has a single real root (-1) which is repeated twice.
-
For the quadratic equation 7x^2 + 3x + 2 = 0, the output shows:
Given a: 7.000000 Given b: 3.000000 Given c: 2.000000 X = -0.214286 + 0.489690i X = -0.214286 - 0.489690i
This indicates that the quadratic equation has two distinct complex roots.
-
For the quadratic equation 5x^2 + 7x + 2 = 0, the output shows:
Given a: 5.000000 Given b: 7.000000 Given c: 2.000000 X = -0.400000 X = -1.000000
This indicates that the quadratic equation has two distinct real roots.
Time Complexity
The time complexity of the given code is O(1) because the operations performed are constant for each quadratic equation. Calculating the discriminant, finding square root, and performing arithmetic operations take constant time regardless of the values of 'a', 'b', and 'c'. As a result, the time complexity does not depend on the size of the input.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment