Dada una ecuación con valor a, b y c, donde a y b son cualquier valor y c es constante, ¿cuántas soluciones tiene esta ecuación cuadrática?
Ejemplos:
Input : Output : 2 solutions Input : Output : no solution
Solución:
Para comprobar si la ecuación tiene solución o no, se utiliza la fórmula cuadrática para el discriminante.
La fórmula se da como,
Las condiciones respectivas se dan como,
- si el discriminante es positivo , entonces la ecuación cuadrática tiene dos soluciones.
- si el discriminante es igual , entonces la ecuación cuadrática tiene una solución.
- si el discriminante es negativo , entonces la ecuación cuadrática no tiene solución.
Programas:
C++
// 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; }
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); } }
Python3
# 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
<?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 ?>
Javascript
<script> // Javascript 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) 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 Code var a = 2, b = 5, c = 2; checkSolution(a, b, c); // This code is contributed by Ankita saini </script>
Producción:
2 solutions
Publicación traducida automáticamente
Artículo escrito por bilal-hungund y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA