C Program To Find Quadratic Equation

Posted : admin On 18.08.2021

The C program to solve quadratic equations in standard form is a simple program based on the quadratic formula. Given the coefficients as input, it will solve the equation and output the roots of the equation. This is a simple program is intended for intermediate level C programmers. The source code to find the root of a Quadratic equation is given below. The given program is compiled and executed successfully on Microsoft Visual Studio. //C# program to find the root of a Quadratic Equation. Using System; class QuadRoot public static void ComputeRoot (double a, double b, double c) double root1 = 0; double root2 = 0. Output Explanation. First, let us get to know about quadratic equation, Quadratic Equation in algebraic form is any equation that can be reformed in standard form in which “x” is an unknown number and “a, b and c” are the known numbers, where “a is not equal to 0” and if “a=0” then it’s a linear equation and not a quadratic because there is no numeric term. 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.

  • Write a C program to find roots of a quadratic equation.

A quadratic equation is a second order equation having a single variable. Any quadratic equation can be represented as ax2 + bx + c = 0, where a, b and c are constants( a can't be 0) and x is unknown variable.

For Example
2x2 + 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.
Discriminant(D) valueDescription
D < 0We will get two complex roots.
D = 0We will get two equal roots.
D > 0We will get two real numbers.

C program to find all roots of a quadratic equation

Program Output
Related Topics
C Program to calculate factorial of a number
C program to check year is leap year or not
C program to check whether a number is prime or not
C program to find sum of digits of a number using recursion
C program to find sum of all even numbers between 1 to N
C program to find perfect numbers between 1 to N using for loop
C program to find perfect numbers between 1 to N using for loop
C program to calculate power of a number
C Program to find nPr and nCr
List of all C programs


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 Program To Find Quadratic Equation
  1. When it is greater than Zero, we will get two Real Solutions.
  2. When it is equal to zero, we will get one Real Solution.
  3. 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;

C++ Program To Find Roots Of Quadratic Equation

using System.Collections.Generic;

using System.Text;

namespace SoftwareAndFinance

{

C program to find roots of quadratic equation using pointers

classMath

{

// quadratic equation is a second order of polynomial equation in a single variable

// x = [ -b +/- sqrt(b^2 - 4ac) ] / 2aCitrix workspace on ipad.

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);

C Program To Find Quadratic Equation Of A Number

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 . . .




C Program To Find Quadratic Equation Using Switch Case



C Program To Get Quadratic Equation