📜  查找二次方程中解数的程序

📅  最后修改于: 2021-04-22 09:15:57             🧑  作者: Mango

给定一个方程ax^{2}\pm bx\pm c=0取值a,b和c,其中a和b为任意值,c为常数,求出这个二次方程式有多少个解?

例子:

输入 : 2x^{2}+5x+2=0输出:2个解决方案输入: x^{2}+x+1=0输出:无解

解决方案:
为了检查方程式是否具有解,使用判别式的二次方程式。

给出的条件分别为

  • 如果判别为肯定 b^{2}-4ac> 0 ,则二次方程式有两个解。
  • 如果判别相等 b^{2}-4ac= 0 ,那么二次方程只有一个解。
  • 如果判别为否定 b^{2}-4ac< 0 ,则二次方程式无解。

程式:

C++
// C++ Program to find the solutions of specified equations
#include 
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;
}


Java
// 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);
    }
}


Python 3
# 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


C#
// 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


PHP
 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
?>


输出:

2 solutions