- C++ Program To Find Quadratic Equation
- C Program To Find Roots Of Quadratic Equation Using Pointers
- Find Quadratic Equation Given Points
- C Program To Find Root Of Quadratic Equation
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).
x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
We have to find the value of (b*b - 4*a*c).
- 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.
2x 2 + 5x + 3 = 0 is a quadratic equation where a, b and c are 2, 5 and 3 respectively. To calculate the roots of quadratic equation we can use below formula. There are two solutions of a quadratic equation. X = (-2a + sqrt(D))/2 x = (-2a - sqrt(D))/2 where, D is Discriminant, which differentiate the nature of the roots of quadratic equation. Before going to the program first let us understand what is a Quadratic Equation? Quadratic Equation: A Quadratic Equation is any equation having the form: where x represents an unknown, and a, b, and c represent known numbers such that a is not equal to 0. If a = 0, then the equation is linear equation, not quadratic.
![C Program To Find Quadratic Equation C Program To Find Quadratic Equation](https://2.bp.blogspot.com/-qAVjZov0mkk/V06yGfST5JI/AAAAAAAAAcE/nTFcaLE0rRgd2ld343fsYZT1Hlb2qFruACLcB/s1600/solution%2Bof%2Ba%2Bquadratic%2Bequation_000001.jpg)
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;
{
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
Tortoisesvn client for mac. Two Real Solutions: 1.6667 or -3.5000
Two Real Solutions: -0.2000 or -1.0000
C++ Program To Find Quadratic Equation
One Real Solution: -1.0000
Two Imaginary Solutions: -0.2000 + 0.4000 i or -0.2000 + 0.4000 i
Acoustic solutions lcdw19hdf manual. Press any key to continue . . .
C BasicsC Programming TutorialData Structures |
- C Programming Exercises
C program to find all roots of a quadratic equation using switch case
Write a C program to find all roots of a Quadratic equation using switch case. How to find all roots of a quadratic equation using switch case in C programming. Logic to calculate roots of quadratic equation in C program.
Example
Input
Input a: 4
Input b: -2
Input c: -10
Output
Root1: 1.85
Root2: -1.35
Required knowledge
Basic C programming, Relational operators, Switch case statement
Quadratic equation
In elementary algebra quadratic equation is an equation in the form of
Solving quadratic equation
A quadratic equation can have either one or two distinct real or complex roots depending upon nature of discriminant of the equation. Where discriminant of the quadratic equation is given by
Depending upon the nature of the discriminant, formula for finding roots can be given as:
Case 1: If discriminant is positive. Then there are two real distinct roots given by.
![C Program To Find Quadratic Equation C Program To Find Quadratic Equation](https://www.shaalaa.com/images/_4:e8faf71ea0b14edcaa634e8d008cbe95.png)
Case 2: If discriminant is zero. Then it have exactly one real root given by.
Case 3: If discriminant is negative. Then it will have two distinct complex roots given by.
Logic to find roots of quadratic equation using switch..case
Step by step descriptive logic to find roots of quadratic equation using switch case.
1.Input coefficients of quadratic equation. Store it in some variable say a, b and c.
2.Find discriminant of given equation using formula i.e. discriminant = (b * b) - (4 * a * c).
You can also use pow() function to find square of b.
3.Compute the roots based on the nature of discriminant. Switch the value of switch(discriminant > 0).
4.The expression (discriminant > 0) can have two possible cases i.e. case 0 and case 1.
5.For case 1 means discriminant is positive. Apply formula root1 = (-b + sqrt(discriminant)) / (2*a); to compute root1 and root2 = (-b - sqrt(discriminant)) / (2*a); to compute root2.
6.For case 0 means discriminant is either negative or zero. There exist one more condition to check i.e. switch(discriminant < 0).
7.Inside case 0 switch the expression switch(discriminant < 0).
8.For the above nested switch there are two possible cases. Which is case 1 and case 0. case 1 means discriminant is negative. Whereas case 0 means discriminant is zero.
C Program To Find Roots Of Quadratic Equation Using Pointers
9.Apply the formula to compute roots for both the inner cases.
Find Quadratic Equation Given Points
Program to find roots of quadratic equation using switch..case
C Program To Find Root Of Quadratic Equation
Output
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 4 -2 -10
Two distinct and real roots exists: 1.85 and -1.35