Program to find number of solutions in Quadratic Equation
Last Updated :
30 Aug, 2022
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 solutionsInput : 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++
#include <iostream>
using namespace std;
void checkSolution( int a, int b, int c)
{
if (((b * b) - (4 * a * c)) > 0)
cout << "2 solutions" ;
else if (((b * b) - (4 * a * c)) == 0)
cout << "1 solution" ;
else
cout << "No solutions" ;
}
int main()
{
int a = 2, b = 5, c = 2;
checkSolution(a, b, c);
return 0;
}
|
Java
public class GFG {
static void checkSolution( int a, int b, int c)
{
if (((b * b) - ( 4 * a * c)) > 0 )
System.out.println( "2 solutions" );
else if (((b * b) - ( 4 * a * c)) == 0 )
System.out.println( "1 solution" );
else
System.out.println( "No solutions" );
}
public static void main(String[] args)
{
int a = 2 , b = 5 , c = 2 ;
checkSolution(a, b, c);
}
}
|
Python3
def checkSolution(a, b, c) :
if ((b * b) - ( 4 * a * c)) > 0 :
print ( "2 solutions" )
elif ((b * b) - ( 4 * a * c)) = = 0 :
print ( "1 solution" )
else :
print ( "No solutions" )
if __name__ = = "__main__" :
a, b, c = 2 , 5 , 2
checkSolution(a, b, c)
|
C#
using System;
class GFG
{
static void checkSolution( int a, int b, int c)
{
if (((b * b) - (4 * a * c)) > 0)
Console.WriteLine( "2 solutions" );
else if (((b * b) - (4 * a * c)) == 0)
Console.WriteLine( "1 solution" );
else
Console.WriteLine( "No solutions" );
}
public static void Main()
{
int a = 2, b = 5, c = 2;
checkSolution(a, b, c);
}
}
|
PHP
<?php
function checkSolution( $a , $b , $c )
{
if ((( $b * $b ) - (4 * $a * $c )) > 0)
echo "2 solutions" ;
else if ((( $b * $b ) -
(4 * $a * $c )) == 0)
echo "1 solution" ;
else
echo "No solutions" ;
}
$a = 2; $b = 5; $c = 2;
checkSolution( $a , $b , $c );
?>
|
Javascript
<script>
function checkSolution(a, b, c)
{
if (((b * b) - (4 * a * c)) > 0)
document.write( "2 solutions" );
else if (((b * b) - (4 * a * c)) == 0)
document.write( "1 solution" );
else
document.write( "No solutions" );
}
var a = 2, b = 5, c = 2;
checkSolution(a, b, c);
</script>
|
Output:
2 solutions
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...