Program to find number of solutions in Quadratic Equation

Given an equation ax^{2}\pm bx\pm c=0 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 : 2x^{2}+5x+2=0
Output : 2 solutions

Input : x^{2}+x+1=0
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, b^{2}-4ac

Respective conditions are given as,

  • if the discriminant is positive  b^{2}-4ac> 0, then the quadratic equation has two solutions.
  • if the discriminant is equal  b^{2}-4ac= 0, then the quadratic equation has one solution.
  • if the discriminant is negative  b^{2}-4ac< 0, then the quadratic equation has no solution.

Programs:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the solutions of specified equations
#include <iostream>
using namespace std;
  
// Method to check for solutions of equations
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)
        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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the solutions of specified equations
public 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);
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find the 
# solutions of specified equations 
  
# function to check for
# solutions of equations 
def 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 code
if __name__ == "__main__" :
  
    a, b, c = 2, 5, 2
    checkSolution(a, b, c)
  
# This code is contributed
# by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the solutions 
// of specified equations
using System;
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)
        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 Code
public static void Main()
{
    int a = 2, b = 5, c = 2;
    checkSolution(a, b, c);
}
}
  
// This code is contributed by inder_verma

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to find the solutions 
// of specified equations
  
// Method to check for solutions 
// of equations
function 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
?>

chevron_right



Output:

2 solutions


My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : inderDuMCA, AnkitRai01