Program to find roots of quadratic equation
- Write a program to find roots of quadratic equation in C
- Write a program to find roots of quadratic equation in C++
- Write a program to find roots of quadratic equation in Python
- Write a program to find roots of quadratic equation in PHP
- Write a program to find roots of quadratic equation in Java
- Write a program to find roots of quadratic equation in Java Script
- Write a program to find roots of quadratic equation in C#
Explanation:
To find the roots of a quadratic equation ax2 + bx + c = 0, you can use the quadratic formula:
x = (-b ± sqrt(b2 – 4ac)) / 2a
Steps to Find Roots
- Identify coefficients: Extract the coefficients a, b, and c from the equation.
- Calculate the discriminant:Discriminant(D) = b2 − 4ac
- The discriminant determines the nature of the roots:
- If D > 0: Two distinct real roots.
- If D = 0: Two identical real roots (repeated root).
- If D < 0: Two complex (imaginary) roots.
- The discriminant determines the nature of the roots:
if D >= 0:
x1 = (-b + sqrt(D)) / 2a
x2 = (-b – sqrt(D)) / 2a,
Program to find roots of quadratic equation
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <math.h> int main() { float a, b, c, discriminant, root1, root2; // Input the coefficients of the quadratic equation printf("Enter coefficients a, b and c: "); scanf("%f %f %f", &a, &b, &c); // Calculate discriminant discriminant = b * b - 4 * a * c; // Check if discriminant is positive, zero, or negative if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and distinct: x1 = %.2f, x2 = %.2f\n", root1, root2); } else if (discriminant == 0) { root1 = -b / (2 * a); printf("Roots are real and equal: x1 = x2 = %.2f\n", root1); } else { printf("No real roots.\n"); } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { float a, b, c, discriminant, root1, root2; // Input the coefficients of the quadratic equation cout << "Enter coefficients a, b and c: "; cin >> a >> b >> c; // Calculate discriminant discriminant = b * b - 4 * a * c; // Check if discriminant is positive, zero, or negative if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); cout << "Roots are real and distinct: x1 = " << root1 << ", x2 = " << root2 << endl; } else if (discriminant == 0) { root1 = -b / (2 * a); cout << "Roots are real and equal: x1 = x2 = " << root1 << endl; } else { cout << "No real roots." << endl; } return 0; }
import math # Input the coefficients of the quadratic equation a = float(input("Enter coefficient a: ")) b = float(input("Enter coefficient b: ")) c = float(input("Enter coefficient c: ")) # Calculate discriminant discriminant = b * b - 4 * a * c # Check if discriminant is positive, zero, or negative if discriminant > 0: root1 = (-b + math.sqrt(discriminant)) / (2 * a) root2 = (-b - math.sqrt(discriminant)) / (2 * a) print(f"Roots are real and distinct: x1 = {root1}, x2 = {root2}") elif discriminant == 0: root1 = -b / (2 * a) print(f"Roots are real and equal: x1 = x2 = {root1}") else: print("No real roots.")
<?php // Input the coefficients of the quadratic equation $a = (float)readline("Enter coefficient a: "); $b = (float)readline("Enter coefficient b: "); $c = (float)readline("Enter coefficient c: "); // Calculate discriminant $discriminant = $b * $b - 4 * $a * $c; // Check if discriminant is positive, zero, or negative if ($discriminant > 0) { $root1 = (-$b + sqrt($discriminant)) / (2 * $a); $root2 = (-$b - sqrt($discriminant)) / (2 * $a); echo "Roots are real and distinct: x1 = $root1, x2 = $root2\n"; } elseif ($discriminant == 0) { $root1 = -$b / (2 * $a); echo "Roots are real and equal: x1 = x2 = $root1\n"; } else { echo "No real roots.\n"; } ?>
import java.util.Scanner; public class QuadraticEquation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input the coefficients of the quadratic equation System.out.print("Enter coefficient a: "); double a = scanner.nextDouble(); System.out.print("Enter coefficient b: "); double b = scanner.nextDouble(); System.out.print("Enter coefficient c: "); double c = scanner.nextDouble(); // Calculate discriminant double discriminant = b * b - 4 * a * c; // Check if discriminant is positive, zero, or negative if (discriminant > 0) { double root1 = (-b + Math.sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.sqrt(discriminant)) / (2 * a); System.out.println("Roots are real and distinct: x1 = " + root1 + ", x2 = " + root2); } else if (discriminant == 0) { double root1 = -b / (2 * a); System.out.println("Roots are real and equal: x1 = x2 = " + root1); } else { System.out.println("No real roots."); } scanner.close(); } }
let a = parseFloat(prompt("Enter coefficient a: ")); let b = parseFloat(prompt("Enter coefficient b: ")); let c = parseFloat(prompt("Enter coefficient c: ")); // Calculate discriminant let discriminant = b * b - 4 * a * c; // Check if discriminant is positive, zero, or negative if (discriminant > 0) { let root1 = (-b + Math.sqrt(discriminant)) / (2 * a); let root2 = (-b - Math.sqrt(discriminant)) / (2 * a); console.log(`Roots are real and distinct: x1 = ${root1}, x2 = ${root2}`); } else if (discriminant === 0) { let root1 = -b / (2 * a); console.log(`Roots are real and equal: x1 = x2 = ${root1}`); } else { console.log("No real roots."); }
using System; class Program { static void Main() { // Input the coefficients of the quadratic equation Console.Write("Enter coefficient a: "); double a = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter coefficient b: "); double b = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter coefficient c: "); double c = Convert.ToDouble(Console.ReadLine()); // Calculate discriminant double discriminant = b * b - 4 * a * c; // Check if discriminant is positive, zero, or negative if (discriminant > 0) { double root1 = (-b + Math.Sqrt(discriminant)) / (2 * a); double root2 = (-b - Math.Sqrt(discriminant)) / (2 * a); Console.WriteLine($"Roots are real and distinct: x1 = {root1}, x2 = {root2}"); } else if (discriminant == 0) { double root1 = -b / (2 * a); Console.WriteLine($"Roots are real and equal: x1 = x2 = {root1}"); } else { Console.WriteLine("No real roots."); } } }