Program to find number of solutions in Quadratic Equation

22 Apr, 2021
Given an equation with value a, b, and c, where a and b is any value and c is constant, find how many solutions thus this quadratic equation have?Examples:  
Input : 
Output : 2 solutions
Input : 
Output : no solution
Solution: To check whether the equation has a solution or not, quadratic formula for discriminant is used. 
The formula is given as, 
Respective conditions are given as,  
if the discriminant is positive , then the quadratic equation has two solutions.
if the discriminant is equal , then the quadratic equation has one solution.
if the discriminant is negative , then the quadratic equation has no solution.
Programs:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the solutions of specified equations#include <iostream>using namespace std; // Method to check for solutions of equationsvoid checkSolution(int a, int b, int c){     // If the expression is greater than 0, then 2 solutions    if (((b * b) - (4 * a * c)) > 0)        cout << "2 solutions";     // If the expression is equal 0, then 2 solutions    else if (((b * b) - (4 * a * c)) == 0)        cout << "1 solution";     // Else no solutions    else        cout << "No solutions";} int main(){    int a = 2, b = 5, c = 2;    checkSolution(a, b, c);    return 0;}
// Java Program to find the solutions of specified equationspublic class GFG {     // Method to check for solutions of equations    static void checkSolution(int a, int b, int c)    {         // If the expression is greater than 0,        // then 2 solutions        if (((b * b) - (4 * a * c)) > 0)            System.out.println("2 solutions");         // If the expression is equal 0, then 2 solutions        else if (((b * b) - (4 * a * c)) == 0)            System.out.println("1 solution");         // Else no solutions        else            System.out.println("No solutions");    }     // Driver Code    public static void main(String[] args)    {        int a = 2, b = 5, c = 2;        checkSolution(a, b, c);    }}
# Python3 Program to find the# solutions of specified equations # function to check for# solutions of equationsdef checkSolution(a, b, c) :     # If the expression is greater    # than 0, then 2 solutions    if ((b * b) - (4 * a * c)) > 0 :        print("2 solutions")     # If the expression is equal 0,    # then 1 solutions    elif ((b * b) - (4 * a * c)) == 0 :        print("1 solution")     # Else no solutions    else :        print("No solutions") # Driver codeif __name__ == "__main__" :     a, b, c = 2, 5, 2    checkSolution(a, b, c) # This code is contributed# by ANKITRAI1
// C# Program to find the solutions// of specified equationsusing System;class GFG{ // Method to check for solutions of equationsstatic void checkSolution(int a, int b, int c){     // If the expression is greater    // than 0, then 2 solutions    if (((b * b) - (4 * a * c)) > 0)        Console.WriteLine("2 solutions");     // If the expression is equal to 0,    // then 2 solutions    else if (((b * b) - (4 * a * c)) == 0)        Console.WriteLine("1 solution");     // Else no solutions    else        Console.WriteLine("No solutions");} // Driver Codepublic static void Main(){    int a = 2, b = 5, c = 2;    checkSolution(a, b, c);}} // This code is contributed by inder_verma
<?php// Program to find the solutions// of specified equations // Method to check for solutions// of equationsfunction checkSolution($a, $b, $c){     // If the expression is greater    // than 0, then 2 solutions    if ((($b * $b) - (4 * $a * $c)) > 0)        echo "2 solutions";     // If the expression is equal 0,    // then 2 solutions    else if ((($b * $b) -              (4 * $a * $c)) == 0)        echo "1 solution";     // Else no solutions    else        echo"No solutions";} // Driver Code$a = 2; $b = 5; $c = 2;checkSolution($a, $b, $c); // This code is contributed// by inder_verma?>
<script> // Javascript Program to find the solutions// of specified equations // Method to check for solutions of equationsfunction checkSolution(a, b, c){         // If the expression is greater than 0,    // then 2 solutions    if (((b * b) - (4 * a * c)) > 0)        document.write("2 solutions");     // If the expression is equal 0, then 2 solutions    else if (((b * b) - (4 * a * c)) == 0)        document.write("1 solution");     // Else no solutions    else        document.write("No solutions");} // Driver Codevar a = 2, b = 5, c = 2; checkSolution(a, b, c); // This code is contributed by Ankita saini </script>
Output: 
2 solutions
 
inderDuMCA
ankthon
ankita_saini
Algebra
Technical Scripter 2018
Mathematical
School Programming
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java