Find square root of quadratic equation

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


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







© 2021, kalkicode.com, All rights reserved