C Program to find the roots of quadratic equation. Quadratic equations are the polynomial equation with degree 2. It is represented as ax 2 + bx +c = 0, where a, b and c are the coefficient variable of the equation. The universal rule of quadratic equation defines that the value of 'a' cannot be zero, and the value of x is used to find the.
I have given here a C# program to solve any Quadratic Equation. Quadratic equation is a second order of polynomial equation in a single variable.
x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
We have to find the value of (b*b - 4*a*c).
- C if.else Statement The standard form of a quadratic equation is: ax 2 + bx + c = 0, where a, b and c are real numbers and a!= 0 The term b 2 -4ac is known as the discriminant of a quadratic equation.
- Here we will a write a program to solve quadratic equation in C programming language. Let’s first understand how to solve a quadratic equation: Let’s take the quadratic equation as: ax²+bx+c=0. How program works: Take the values for a, b and c (double type). If, a = b = 0 then invalid equation; if b² -4ac.
- When it is greater than Zero, we will get two Real Solutions.
- When it is equal to zero, we will get one Real Solution.
- When it is less than Zero, we will get two Imaginary Solutions.
When there is an imaginary solutions, we have to use the factor i to represent imaginary part as it is a complex number.
Source Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SoftwareAndFinance
{
classMath
{
// quadratic equation is a second order of polynomial equation in a single variable
// x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
publicstaticvoid SolveQuadratic(double a, double b, double c)
{
double sqrtpart = b * b - 4 * a * c;
double x, x1, x2, img;
if (sqrtpart > 0)
{
x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
Console.WriteLine('Two Real Solutions: {0,8:f4} or {1,8:f4}', x1, x2);
}
elseif (sqrtpart < 0)
{
sqrtpart = -sqrtpart;
x = -b / (2 * a);
img = System.Math.Sqrt(sqrtpart) / (2 * a);
Console.WriteLine('Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i', x, img, x, img);
}
else
{
x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
Console.WriteLine('One Real Solution: {0,8:f4}', x);
}
}
staticvoid Main(string[] args)
{
// 6x^2 + 11x - 35 = 0
SolveQuadratic(6, 11, -35);
// 5x^2 + 6x + 1 = 0
SolveQuadratic(5, 6, 1);
// 2x^2 + 4x + 2 = 0
SolveQuadratic(2, 4, 2);
// 5x^2 + 2x + 1 = 0
SolveQuadratic(5, 2, 1);
}
}
}
Output
Two Real Solutions: 1.6667 or -3.5000
Two Real Solutions: -0.2000 or -1.0000
One Real Solution: -1.0000
Two Imaginary Solutions: -0.2000 + 0.4000 i or -0.2000 + 0.4000 i
Press any key to continue . . .
Here we will a write a program to solve quadratic equation in C++ programming language. Let’s first understand how to solve a quadratic equation:
x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
Console.WriteLine('Two Real Solutions: {0,8:f4} or {1,8:f4}', x1, x2);
}
elseif (sqrtpart < 0)
{
sqrtpart = -sqrtpart;
x = -b / (2 * a);
img = System.Math.Sqrt(sqrtpart) / (2 * a);
Console.WriteLine('Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i', x, img, x, img);
}
else
{
x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
Console.WriteLine('One Real Solution: {0,8:f4}', x);
}
}
staticvoid Main(string[] args)
{
// 6x^2 + 11x - 35 = 0
SolveQuadratic(6, 11, -35);
// 5x^2 + 6x + 1 = 0
SolveQuadratic(5, 6, 1);
// 2x^2 + 4x + 2 = 0
SolveQuadratic(2, 4, 2);
// 5x^2 + 2x + 1 = 0
SolveQuadratic(5, 2, 1);
}
}
}
Output
Two Real Solutions: 1.6667 or -3.5000
Two Real Solutions: -0.2000 or -1.0000
One Real Solution: -1.0000
Two Imaginary Solutions: -0.2000 + 0.4000 i or -0.2000 + 0.4000 i
Press any key to continue . . .
Here we will a write a program to solve quadratic equation in C++ programming language. Let’s first understand how to solve a quadratic equation:
Let’s take the quadratic equation as:ax²+bx+c=0
How program works:
- Take the values for a, b and c (double type).
- if, a = b = 0 then invalid equation
- if b² -4ac < 0 then the real roots
- if b² -4ac > 0 , x1= (-b+√(b² -4ac))/2a , x1= (-b -√(b² -4ac))/2a
- The C++ function for √x is sqrt(x).
- Include math.h for using the sqrt() function.
C Program To Find Quadratic Formula
Now let’s write the program for the same.
Here is a program to solve Quadratic equation in Python
Program to solve Quadratic equation in C++
C Program To Find Quadratic Equation Of A Number
OUTPUT:
C Program To Find Roots Of Quadratic Equation Using Functions
Please comment for any concern or suggestions for improvements.