Overview
Next: Write a C# Sharp program to read roll no, name and marks of three subjects. Within this program to find roots of quadratic equation example, User entered Values are 10 15 -25. It means a = 10, b = 15, c = -25 and the Quadratic equation is 10x²+15x-25 = 0. C program to find the roots of a quadratic equation using switch case: The below program ask the user to enter the value of a,b and c. After getting the value from the user it will calculate on the basis of ‘Discriminant' value using switch case. Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1 Roots of quadratic equation are: 0.000, -2.000 Other Related Programs in c C Program to calculate the Combinations and Permutations. C program to find the Roots of Quadratic equation; C Program to Solve any Linear Equation in One Variable; Find number of solutions of a linear equation of n variables in C; C Program to Find All Roots of a Quadratic Equation; Java program to find the roots of a quadratic equation; Secant method to solve non-linear equation Program to.
In this tutorial, we will write a simple C++ Program to Find Quadratic Equation Roots. The roots for the equation can be calculated as shown below:
C Program to find Roots of a Quadratic Equation Using Else If This program allows the user to enter three values for a, b, and c. Next, this c program find roots of a quadratic equation using Else If Statement and sqrt function.
Root 1 =
Root 2=
C++ Program
Sample Output
Root 1 := 6 Root 2 := -2 Process returned 0 (0x0) execution time : 2.083 s Press any key to continue.
Enhancements
We can further enhance the program to prompt the input values of a, b, c to the user. Right now we have to modify the code for the values.
Generate the equation string based on the values of a, b, and c and display the equation string in the output.
Take into consideration the imaginary roots if discriminant is less than 0. The logic is outlined in the flowchart link
Flowchart :
The IDE used in the tutorial is Code:: Blocks. To download and install Code Blocks follow the link:
For more information on Code Blocks IDE used in the program , visit the official website of Code blocks IDE: http://www.codeblocks.org/
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).
- 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);
Premiere pro workspace file location. 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);
C Program To Get Quadratic Equation
}
}
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
C Program To Find Quadratic Formula
SolveQuadratic(2, 4, 2);
// 5x^2 + 2x + 1 = 0
SolveQuadratic(5, 2, 1);
C++ Program To Find Quadratic Equation
}
}
}
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 . . .
C Program To Find Discriminant Of Quadratic Equation
C Program To Find Quadratic Equation Of A Number
C Program To Find Quadratic Equation Using Switch Case
C++ Program To Find Roots Of Quadratic Equation
Console.WriteLine('Two Real Solutions: {0,8:f4} or {1,8:f4}', x1, x2);
}
elseif (sqrtpart < 0)
{
sqrtpart = -sqrtpart;
x = -b / (2 * a);
Premiere pro workspace file location. 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);
C Program To Get Quadratic Equation
}
}
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
C Program To Find Quadratic Formula
SolveQuadratic(2, 4, 2);
// 5x^2 + 2x + 1 = 0
SolveQuadratic(5, 2, 1);
C++ Program To Find Quadratic Equation
}
}
}
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 . . .