C Programming Tutorial: The Basics you Need to Master C

Everything you need to know about basic structure of a c program, how to compile c program in command prompt, how to implement linear search in c, how to write c program to find the roots of a quadratic equation, everything you need to know about sorting algorithms in c, fibonacci series in c : a quick start to c programming, how to reverse number in c, how to implement armstrong number in c, how to carry out swapping of two numbers in c, c program to find lcm of two numbers, leap year program in c, switch case in c: everything you need to know, everything you need to know about pointers in c, how to implement selection sort in c, how to write a c program for deletion and insertion, how to implement heap sort in c, how to implement bubble sort in c, binary search in c: everything you need to know binary search, introduction to c programming-algorithms, what is objective-c: why should you learn it, how to implement static variable in c, how to implement queue in c, how to implement circular queue in c, what is embedded c programming and how is it different.

write program to find roots of quadratic equation

Calculating the roots of a quadratic equation is something that we have learnt in childhood. However, the complexity of quadratic equations increased with time. The methods to deal with this problem have also evolved. In this article, we would take a look a way to write a C program to find the roots of a quadratic equation.

Following are the pointers to be covered in this article:

  • What is a quadratic equation?
  • Types of roots

Understanding the Algorithm

C program to find the roots of a quadratic equation.

Let’s begin.

What is a Quadratic Equation?

Before diving into the coding part of this post, let’s first understand what exactly is a quadratic equation? This section will give you a good understanding of quadratic equations which will help in understanding the algorithm. So, let’s get back to the solution. An equation of degree 2 is known as a quadratic equation. If you’re not familiar with the term ‘degree’, it is an order of an equation and its value is equal to the largest exponent present in an equation.

Standard Form

Let’s see what is the standard form of a quadratic equation, which will help you in identifying them quickly.

ax 2 + bx + c = 0 is the standard form of a quadratic equation where the values of variables a, b, c is already known. The value of x is unknown and a not equal to 0.

When you plot a quadratic equation on a graph, you’ll get a curve (parabola). The points at which this equation cuts the axis are its roots . Generally, there are 2  roots of a quadratic equation. Let’s discuss the details of roots of a quadratic equation. 

Types of Roots

There are a few different ways to find the roots of a given quadratic equation. Such as, f actoring the given quadratic equation, using the complete square formula and the last method involves using the quadratic formula to find the roots. I will stick to the last method as it has fewer drawbacks compared to the other methods.

Now, let’s talk about roots. Basically, the term inside the square-root (b 2 – 4ac) in the numerator is also known as the discriminant. It can tell you more about the nature of the roots of the quadratic equation. Let’s have a look

  • If the value of the discriminant is Positive, that means we have 2 roots which are real in nature.
  • If its value is negative, you have a pair of roots which are Complex in nature (eg- 5i, 8i).
  • If it is 0, you get 2 roots having equal value and real in nature.

Now that you have a deep understanding of quadratic equations and their roots, we are good to start with the algorithm and it actually feels amazing when you’ll implement what you’ve learned in a real-world problem.

Step by step let’s understand how we can write a program that can be used to find the roots of a quadratic equation.

  • First, we will ask the user for the values of a, b, and c to form the quadratic equation.
  • Once you have the values, you need to check if the value of a, entered is not 0. If it’s 0 our x 2 term will be 0 and our equation won’t be quadratic anymore.
  • Now, you have a valid quadratic equation so you can calculate its discriminant value.
  • After getting the discriminant value, you can check for the 3 conditions discussed above to know the nature of the roots.
  • After knowing the nature, you can calculate its roots.

Let’s now look into the C program to find the roots of a quadratic equation.

We received 2 complex roots for the values entered. Next time if you have a complex quadratic equation to solve, this program will definitely save you some time.

That’s all for this article. I hope you have understood the concept and the C program to find the roots of a quadratic equation.

If you come across any questions, feel free to ask all your questions in the comments section of “quadratic equation in C” and our team will be glad to answer.

Recommended videos for you

Microsoft azure certifications – all you need to know, how to crack cfa level 1 exam, nandan nilekani on entrepreneurship, recommended blogs for you, flutter tutorial for mobile developers: a complete tutorial on cross-platform development, discovering the power of design thinking process with edureka, what is the difference between product owner and project manager, what is the importance of performance management all you need to know, 2 million youtube subscribers: the story behind the success, what is domain testing in software testing, azure data engineer interview questions and answers, c# tutorial: the fundamentals you need to master c#, how to use chatgpt-4 everything you need to know, all you need to know about arrays in c programming, ai applications: top 10 real world artificial intelligence applications, how do you make a career in software testing, top c programming interview questions you need to master in 2024, top 30 splunk interview questions to prepare in 2024, what is jmeter api testing and how it works, vol. viii – edureka career watch – 2nd mar. 2019, learn business management courses from the best institution, iim online courses for working professionals: way to upskill, join the discussion cancel reply, trending courses, cyber security and ethical hacking internship ....

  • 15k Enrolled Learners
  • Weekend/Weekday

Full Stack Web Development Internship Program

  • 29k Enrolled Learners

Microsoft Power BI Certification Training Cou ...

  • 74k Enrolled Learners

DevOps Course with Training and Certification

  • 164k Enrolled Learners

Data Science and Machine Learning Internship ...

  • 22k Enrolled Learners

AWS Certification Training Course for Solutio ...

  • 162k Enrolled Learners

PMP Certification Training Course

  • 70k Enrolled Learners

Cyber Security Certification Course

  • 63k Enrolled Learners

Software Testing and Automation Internship Pr ...

  • 1k Enrolled Learners

Human-Computer Interaction (HCI) for AI Syste ...

  • 2k Enrolled Learners

Browse Categories

Subscribe to our newsletter, and get personalized recommendations..

Already have an account? Sign in .

20,00,000 learners love us! Get personalised resources in your inbox.

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

We have recieved your contact details.

You will recieve an email from us shortly.

Codeforwin

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.

Required knowledge

Basic C programming , Relational operators , Switch case statement

Learn more – Program to find roots of quadratic equation using if…else.

Quadratic equation

In elementary algebra quadratic equation is an equation in the form of

Quadratic equation

Solving quadratic equation

Discriminant of a quadratic equation

Depending upon the nature of the discriminant, formula for finding roots can be given as:

Quadratic equation formula root 1

Logic to find roots of quadratic equation using switch...case

Step by step descriptive logic to find roots of quadratic equation using switch case.

  • Input coefficients of quadratic equation. Store it in some variable say a , b and c .
  • 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 .
  • Compute the roots based on the nature of discriminant. Switch the value of switch(discriminant > 0) .
  • The expression (discriminant > 0) can have two possible cases i.e. case 0 and case 1 .
Learn more – Program to find square root of a number.
  • For case 0 means discriminant is either negative or zero. There exist one more condition to check i.e. switch(discriminant < 0) .
  • Inside case 0 switch the expression switch(discriminant < 0) .
  • 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.
  • Apply the formula to compute roots for both the inner cases.

Program to find roots of quadratic equation using switch...case

Happy coding 😉

Recommended posts

  • Switch case programming exercise index
  • C program to print day of week name using switch case .
  • C program to find total number of days in a month using switch case .
  • C program to check positive, negative or zero using switch case.
  • C program to find maximum between two numbers using switch case .
  • C program to check whether a number is even or odd using switch case .
  • C program to create simple calculator using switch case .

Programtopia

C Program to Find the Roots of Quadratic Equation

A quadratic equation is an equation of the form ax 2 + bx + c = 0 where a, b and c are constants.

And the formula to calculate the roots of the quadratic equation is:

Formula to calculate quadratic equation

C program to find the roots of a quadratic equation is shown below.

The values a, b and c are entered by the user. Then the value of x (i.e. the value of b 2 -4ac) is calculated. The keyword sqrt() calculates the square root. The value of

Formula to calculate determinant

is calculated by the statement sqrt (fabs(x)) where fabs means absolute value of float. This value is assigned to variable d. Now, if the value of x is 0 then the process associated with the if condition is executed. If the value of x is greater than 0 then the process associated with the else if condition is executed, otherwise the value associated with the else condition is executed.

This program can also be executed by using the switch statement.

Here if the value of d is zero, then z=1 and if d is greater than zero, then z = 2 otherwise the value of z equals 3. Now the value of z is checked using the switch statement. If the value of z is 1, then the statements associated with case 1 label is executed. If the value of z is 2 then the statements associated with the case 2 label is executed. And finally, if the value of z is 3 then the statements associated with the case 3 label is executed.

Roots of Quadratic Equation

For a given quadratic equation ax 2 + bx + c = 0, the values of x that satisfy the equation are known as its roots. i.e., they are the values of the variable (x) which satisfies the equation. The roots of a quadratic function are the x-coordinates of the x-intercepts of the function. Since the degree of a quadratic equation is 2, it can have a maximum of 2 roots. We can find the roots of quadratic equations using different methods.

  • Factoring (when possible)
  • Quadratic Formula
  • Completing the Square
  • Graphing (used to find only real roots)

Let us understand more about the roots of the quadratic equation along with discriminant, nature of the roots, the sum of roots, the product of roots, and more along with some examples.

The roots of a quadratic equation are the values of the variable that satisfy the equation. They are also known as the "solutions" or "zeros" of the quadratic equation . For example, the roots of the quadratic equation x 2 - 7x + 10 = 0 are x = 2 and x = 5 because they satisfy the equation. i.e., when each of them is substituted in the given equation we get 0.

  • when x = 2, 2 2 - 7(2) + 10 = 4 - 14 + 10 = 0.
  • when x = 5, 5 2 - 7(5) + 10 = 25 - 35 + 10 = 0.

But how to find the roots of a general quadratic equation ax 2 + bx + c = 0? Let us try to solve it for x by completing the square.

ax 2 + bx = - c

Dividing both sides by 'a',

x 2 + (b/a) x = - c/a

Here, the coefficient of x is b/a. Half of it is b/(2a). Its square is b 2 /4a 2 . Adding b 2 /4a 2 on both sides,

x 2 + (b/a) x + b 2 /4a 2 = (b 2 /4a 2 ) - (c/a)

[ x + (b/2a) ] 2 = (b 2 - 4ac) / 4a 2 (using (a + b)^2 formula )

Taking square root on both sides,

x + (b/2a) = ±√ [(b 2 - 4ac) / 4a 2 ]

x + (b/2a) = ±√ (b 2 - 4ac) / 2a

Subtracting b/2a from both sides,

x = - (b/2a) ±√ (b 2 - 4ac) / 2a

x = (-b ± √ (b 2 - 4ac) )/2a

This is known as the quadratic formula and it can be used to find any type of roots of a quadratic equation.

roots of a quadratic equation

How to Find the Roots of Quadratic Equation?

The process of finding the roots of the quadratic equations is known as "solving quadratic equations". In the previous section, we have seen that the roots of a quadratic equation can be found using the quadratic formula. Along with this method, we have several other methods to find the roots of a quadratic equation. To know about these methods in detail, click here . Let us discuss each of these methods here by solving an example of finding the roots of the quadratic equation x 2 - 7x + 10 = 0 (which was mentioned in the previous section) in each case. Note that, in each of these methods, the equation should be in the standard form ax 2 + bx + c = 0.

Finding Roots of Quadratic Equation by Factoring

  • Factor the left side part.
  • Set each of these factors to zero and solve.

Example: Find the roots of the quadratic equation x 2 - 7x + 10 = 0 by factoring.

By factoring the quadratic expression, we get (x - 2) (x - 5) = 0.

Now, setting each factor to zero and solving, we get

x - 2 = 0, x - 5 = 0

x = 2, x = 5.

Finding Roots of Quadratic Equation by Quadratic Formula

  • Find a, b, and c values by comparing the given equation with ax 2 + bx + c = 0.
  • Substitute them in the quadratic formula and simplify.

Example: Find the roots of quadratic equation x 2 - 7x + 10 = 0 using quadratic formula.

Here, a = 1, b = -7 and c = 10. Then by quadratic formula:

x = [-(-7) ± √((-7) 2 - 4(1)(10))] / (2(1))

= [ 7 ± √(49 - 40) ] / 2

= [ 7 ± √(9) ] / 2

= [ 7 ± 3 ] / 2

= (7 + 3) / 2, (7 - 3) / 2

= 10/2, 4/2

Therefore, x = 2, x = 5.

Finding Roots of Quadratic Equation by Completing Square

  • Complete the square on the left side.
  • Solve by taking square root on both sides.

Example: Find the quadratic roots of x 2 - 7x + 10 = 0 by completing square.

By completing the square, we get (x - (7/2) ) 2 = 9/4.

Now, taking the square root on both sides:

x - 7/2 = ± 3/2

x - 7/2 = 3/2, x - 7/2 = -3/2

x = 10/2, x = 4/2

x = 5, x = 2

Finding Roots of Quadratic Equation by Graphing

  • Graph the left side part (the quadratic function ) either manually or using the graphing display calculator (GDC).
  • Identify the x-intercepts which are nothing but the roots of the quadratic equation.

Example: Find the roots of the quadratic equation x 2 - 7x + 10 = 0 by graphing.

Solution: To solve this, we just need to graph f(x) = x 2 - 7x + 10 and identify the x-intercepts.

 finding roots of quadratic equation by graphing

Therefore, the roots of the quadratic equation are x = 2 and x = 5 .

We can observe that the roots of the quadratic equation x 2 - 7x + 10 = 0 are x = 2 and x = 5 in each of the methods. Note that the factoring method works only when the quadratic equation is factorable; and we cannot find the complex roots of the quadratic equation using the graphing method. So the best methods that always work for finding the roots are the quadratic root formula and completing the square methods.

Nature of Roots of Quadratic Equation

The nature of the roots of a quadratic equation talks about "how many roots the equation has?" and "what type of roots the equation has?". A quadratic equation can have:

  • two real and different roots
  • two complex roots
  • two real and equal roots (it means only one real root)

For example, in the above example, the roots of the quadratic equation x 2 - 7x + 10 = 0 are x = 2 and x = 5, where both 2 and 5 are two different real numbers , and so we can say that the equation has two real and different roots. But for finding the nature of the roots, we don't actually need to solve the equation. We can determine the nature of the roots by using the discriminant . The discriminant of the quadratic equation ax 2 + bx + c = 0 is D = b 2 - 4ac .

The roots of quadratic equation formula is x = (-b ± √ (b 2 - 4ac) )/2a. So this can be written as x = (-b ± √ D )/2a. Since the discriminant D is in the square root , we can determine the nature of the roots depending on whether D is positive, negative, or zero.

Nature of Roots When D > 0

Then the above formula becomes,

x = (-b ± √ positive number )/2a and it gives us two real and different roots. Thus, the quadratic equation has two real and different roots when b 2 - 4ac > 0.

Nature of Roots When D < 0

x = (-b ± √ negative number )/2a and it gives us two complex roots (which are different) as the square root of a negative number is a complex number . Thus, the quadratic equation has two complex roots when b 2 - 4ac < 0.

Note: A quadratic equation can never have one complex root. The complex roots always occur in pairs. i.e., if a + bi is a root then a - bi is also a root.

Nature of Roots When D = 0

x = (-b ± √ 0 )/2a = -b/2a

and hence the equation has only one real root. Thus, the quadratic equation has only one real root (or two equal roots -b/2a and -b/2a) when b 2 - 4ac = 0.

Sum and Product of Roots of Quadratic Equation

We have seen that the roots of the quadratic equation x 2 - 7x + 10 = 0 are x = 2 and x = 5. So the sum of its roots = 2 + 5 = 7 and the product of its roots = 2 × 5 = 10. But the sum and the product of roots of a quadratic equation ax 2 + bx + c = 0 can be found without actually calculating the roots. Let us see how.

We know that the roots of an equation ax 2 + bx + c = 0 by quadratic formula are (-b + √ (b 2 - 4ac)) /2a and (-b - √ (b 2 - 4ac) )/2a. Let us represent these by x 1 and x 2 respectively.

Sum of Roots of Quadratic Equation

The sum of the roots = x 1 + x 2

= (-b + √ (b 2 - 4ac)) /2a + (-b - √ (b 2 - 4ac) )/2a

= -b/2a - b/2a

Therefore, the sum of the roots of the quadratic equation ax 2 + bx + c = 0 is -b/a.

For the equation, x 2 - 7x + 10 = 0, the sum of the roots = -(-7)/1 = 7 (which was the sum of the actual roots 2 and 5).

Product of Roots of Quadratic Equation

The product of the roots = x 1 · x 2

= (-b + √ (b² - 4ac) )/2a · (-b - √ (b² - 4ac) )/2a

= (-b/2a) 2 - ( √ (b 2 - 4ac)/ 2a) 2 ( by a² - b² formula )

= b 2 / 4a 2 - (b 2 - 4ac) / 4a 2

= (b 2 / 4a 2 ) - (b 2 / 4a 2 ) + (4ac / 4a 2 )

= 4ac / 4a 2

Therefore, the product of the roots of the quadratic equation ax 2 + bx + c = 0 is c/a.

For the equation, x 2 - 7x + 10 = 0, the product of the roots = 10/1 = 10 (which was the product of the actual roots 2 and 5).

Important Formulas on Roots of Quadratic Equations:

For a quadratic equation ax 2 + bx + c = 0,

  • The roots are calculated using the formula, x = (-b ± √ (b 2 - 4ac) )/2a.
  • Discriminant is, D = b 2 - 4ac. If D > 0, then the equation has two real and distinct roots. If D < 0, the equation has two complex roots. If D = 0, the equation has only one real root.
  • Sum of the roots = -b/a
  • Product of the roots = c/a

☛ Related Topics:

  • Roots of Quadratic Equation Calculator
  • Quadratic Root Formula Calculator
  • Quadratic Roots by Completing Square Calculator

Examples on Roots of Quadratic Equation

Example 1: Find the roots of the quadratic equation √2 p 2 + 7p + 5√2 = 0.

Let us find the roots using the factoring method.

Comparing the given equation with ap 2 + bp + c = 0, a = √2, b = 7 and c = 5√2.

Here, ac = (√2) (5√2) = 10 and b = 7.

Two numbers whose sum is 7 and whose product is 10 are 2 and 5. Let us split the middle term using these numbers.

√2 p 2 + 2p + 5p + 5√2 = 0

√2 p (p + √2) + 5 (p + √2) = 0

(p + √2) (√2 p + 5) = 0

p + √2 = 0; √2 p + 5 = 0

p = -√2; p = -5/√2 (or) -5√2/2

Answer: The roots of the given quadratic equation are -√2 and -5√2 / 2.

Example 2: Find the value(s) of k if the quadratic equation 3x 2 + kx + 2 = 0 has equal roots.

The quadratic equation ax 2 + bx + c = 0 has equal roots if its discriminant b 2 - 4ac = 0.

Here, a = 3, b = k, and c = 2.

b 2 - 4ac = 0

k 2 - 4(3)(2) = 0

k 2 - 24 = 0

Answer: When the given quadratic equation has equal roots, k = 2√6 or k = -2√6.

Example 3: Find the sum and the product of the roots of the equation (p + 1) x 2 - 2px + (q + 1) = 0 in terms p and q.

Comparing the given equation with ax 2 + bx + c = 0,

a = p + 1, b = -2p, and c = q + 1.

The sum of the roots = -b/a = -(-2p) / (p + 1) = 2p / (p + 1).

The product of the roots = c/a = (q + 1) / (p + 1).

Answer: The sum of the roots is 2p / (p + 1) and the product of the roots is (q + 1) / (p + 1).

go to slide go to slide go to slide

write program to find roots of quadratic equation

Book a Free Trial Class

Practice Questions on Roots of Quadratic Equation

go to slide go to slide

FAQs on Roots of Quadratic Equation

What are the roots of a quadratic equation.

The roots of a quadratic equation ax 2 + bx + c = 0 are the values of the variable (x) that satisfy the equation. For example, the roots of the equation x 2 + 5x + 6 = 0 are -2 and -3. The roots of an equation ax 2 + bx + c = 0 can be found by the quadratic formula x = (-b ± √ (b 2 - 4ac)) /2a.

How Can We Find the Roots of Quadratic Equation?

The roots of a quadratic equation ax 2 + bx + c = 0 can be found using the quadratic formula that says x = (-b ± √ (b 2 - 4ac)) /2a. Alternatively, if the quadratic expression is factorable, then we can factor it and set the factors to zero to find the roots.

What are the Three Types of Roots of Roots of Quadratic Equation?

A quadratic equation ax 2 + bx + c = 0 can have:

  • two real and distinct roots when b 2 - 4ac > 0.
  • two complex roots when b 2 - 4ac < 0.
  • two real and equal roots when b 2 - 4ac = 0.

How to Find the Roots of Quadratic Equation by Completing Square?

To find the roots of a quadratic equation ax 2 + bx + c = 0 by completing square, complete the square on the left side first. Then solve for x by taking the square root on both sides.

Where to Find Quadratic Root calculator?

The quadratic root calculator can be found by clicking here . This allows us to enter a quadratic equation and then it shows the roots along with step-by-step calculation.

How to Determine the Nature of Roots of Quadratic Equation?

The nature of the roots of an equation ax 2 + bx + c = 0 is determined by its discriminant, D = b 2 - 4ac.

  • If D > 0, the equation has two real and distinct roots.
  • If D < 0, the equation has two complex roots.
  • If D = 0, the equation has two equal real roots.

How to Find the Roots of Quadratic Equation Using Quadratic Formula?

The quadratic formula says the roots of a quadratic equation ax 2 + bx + c = 0 are given by x = (-b ± √ (b 2 - 4ac)) /2a. To solve any quadratic equation, convert it into standard form ax 2 + bx + c = 0, find the values of a, b, and c, substitute them in the roots of quadratic equation formula and simplify.

How to Find the Sum and Product of Roots of Quadratic Equation?

For any quadratic equation ax 2 + bx + c = 0 whose roots are α and β,

  • the sum of the roots, α + β = -b/a
  • the product of the roots, α × β = c/a

Can Both the Roots of Quadratic Equation be Zeros?

Yes, both the roots of a quadratic equation can be zeros . For example, the two roots of the quadratic equation x 2 = 0 are 0 and 0.

How to Find the Roots of Quadratic Equation by Factoring?

To find the roots of an equation ax 2 + bx + c = 0 by factoring, factor its left side part, set each of the factors to zero and solve.

no-js-running-man-logo

Online Quadratic Formula Calculator

Apply the quadratic formula using wolfram|alpha, a useful tool for finding the solutions to quadratic equations.

Wolfram|Alpha can apply the quadratic formula to solve equations coercible into the form . In doing so, Wolfram|Alpha finds both the real and complex roots of these equations. It can also utilize other methods helpful to solving quadratic equations, such as completing the square, factoring and graphing.

Quadratic equations results with plots, roots and answers

Learn more about:

  • Quadratic formula

Tips for entering queries

Enter your queries using plain English. To avoid ambiguous queries, make sure to use parentheses where necessary. Here are some examples illustrating how to ask about finding roots of quadratic equations.

  • quadratic formula 4x^2 + 4 x - 8
  • quadratic formula a = 1, b = -1, c = 2
  • solve x^2 - x - 4 = 0
  • solve x^2 - 3x - 4 = 0
  • View more examples

Access instant learning tools

Get immediate feedback and guidance with step-by-step solutions and Wolfram Problem Generator

Step-by-step solutions for quadratic equations with alternate methods, informative hints and unlimited Wolfram Problem Generator practice problems

  • Step-by-step solutions
  • Wolfram Problem Generator

What are quadratic equations, and what is the quadratic formula?

A quadratic is a polynomial of degree two..

Quadratic equations form parabolas when graphed, and have a wide variety of applications across many disciplines. In physics, for example, they are used to model the trajectory of masses falling with the acceleration due to gravity.

Situations arise frequently in algebra when it is necessary to find the values at which a quadratic is zero. In other words, it is necessary to find the zeros or roots of a quadratic, or the solutions to the quadratic equation. Relating to the example of physics, these zeros, or roots, are the points at which a thrown ball departs from and returns to ground level.

One common method of solving quadratic equations involves expanding the equation into the form and substituting the , and coefficients into a formula known as the quadratic formula. This formula, , determines the one or two solutions to any given quadratic. Sometimes, one or both solutions will be complex valued.

Discovered in ancient times, the quadratic formula has accumulated various derivations, proofs and intuitions explaining it over the years since its conception. Some involve geometric approaches. Others involve analysis of extrema. There are also many others. Those listed and more are often topics of study for students learning the process of solving quadratic equations and finding roots of equations in general.

Alternative methods for solving quadratic equations do exist. Completing the square, factoring and graphing are some of many, and they have use cases—but because the quadratic formula is a generally fast and dependable means of solving quadratic equations, it is frequently chosen over the other methods.

PrepBytes Blog

ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING

Sign in to your account

Forgot your password?

Login via OTP

We will send you an one time password on your mobile number

An OTP has been sent to your mobile number please verify it below

Register with PrepBytes

C program to find roots of quadratic equation.

' src=

Last Updated on May 10, 2023 by Prepbytes

write program to find roots of quadratic equation

This article will go over a C program for finding the roots of quadratic equations in depth. Finding the roots of the quadratic equation in c will help in building the logic, which will enhance your coding career. Let’s talk about what a quadratic equation is and how to use a C program to find the roots of a quadratic equation. To begin, what exactly is a quadratic equation?

What is a Quadratic Equation in C?

A quadratic equation is an equation of degree 2, which means that the function’s highest exponent is 2. A quadratic has the standard form y = ax2 + bx + c, where a, b, and c are numbers and a cannot be zero. Example of a quadratic equation: 3x^2 + 3x + 1. Let’s look at what quadratic equation roots are and how to get them from the equation.

The term b^2 – 4ac is called the discriminant of a quadratic equation. It tells the nature of the roots.

  • If the discriminant is greater than 0, the roots are real and different.
  • If the discriminant is equal to 0, the roots are real and equal.
  • If the discriminant is less than 0, the roots are complex and different.

write program to find roots of quadratic equation

Algorithm to Find the Roots of Quadratic Equations in C

  • Input the value of a, b, c.
  • Calculate k = b b – 4 a*c
  • Print "Roots are Imaginary, calculate root1 = (-b +i ?k)/ 2a and root2 =(b + i?k)/ 2a.
  • Print "Roots are Equal" and calculate root1 = root2 = (-b / 2*a)
  • Print "Roots are real and calculate root1 = -b + ?d / 2 a and root2 = -b – ?d / 2 a.
  • Print root1 and root2.
  • End the algorithm.

Code Implementation to Find Roots of Quadratic Equations in C

Time Complexity to find roots of quadratic equation in C O(1) will be the time complexity as general mathematics is used to find the roots of the quadratic equation.

Conclusion This article provides the best explanation for solving quadratic equations in C. Solving problems like finding quadratic equation roots on a regular basis will help your programming career. Don’t stop there; try more logical questions like finding the roots of a quadratic equation, which will not only improve your logic but will also improve your programming skills.

Frequently Asked Questions (FAQs)

Q1. How do I find the roots of a number in C? Ans. In C programming, the sqrt() function is a pre-defined library function used to calculate the square root of a number.

Q2. What are the 4 ways to find the roots of a quadratic equation? Ans. There are various methods by which you can solve a quadratic equation, such as: factorization, completing the square, the quadratic formula, and graphing. These are the four general methods by which we can solve a quadratic equation.

Q3. What is an algorithm to solve a quadratic equation? Ans. Quadratic equations are the polynomial equations of degree 2 in one variable of type: f(x) = ax^2 +bx + c, where a, b, c, ∈ R and a ≠ 0. The general form of the quadratic equation is called the leading coefficient, and c is called the absolute term of f(x).

Q4. What is an example of a quadratic equation with real roots? Ans. For the equation x^2-7x+12=0, on solving it, we have the real roots as 3 and 4.

Q5. What is C in quadratic standard form? Ans. In the standard form of a quadratic function, f(x) = ax2 + bx + c, c is equal to the y-intercept of the graph of the function.

Other C Programs C program to calculate percentage of 5 subjects C program to convert binary number to decimal number C program to convert celsius to fahrenheit C program to convert infix to postfix C program to find area of circle C program to add two numbers C program to reverse a number C program for merge sort for linked lists C program for performing bubble sort on linked list C program to reverse a linked list

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

  • Linked List
  • Segment Tree
  • Backtracking
  • Dynamic Programming
  • Greedy Algorithm
  • Operating System
  • Company Placement
  • Interview Tips
  • General Interview Questions
  • Data Structure
  • Other Topics
  • Computational Geometry
  • Game Theory

Related Post

Null character in c, assignment operator in c, ackermann function in c, median of two sorted arrays of different size in c, number is palindrome or not in c, implementation of queue using linked list in c.

Popular Tutorials

Popular examples, reference materials, learn python interactively, javascript examples.

  • Find the Sum of Natural Numbers
  • Check if the Numbers Have Same Last Digit
  • Find HCF or GCD
  • Solve Quadratic Equation
  • Find the Factors of a Number
  • Check if a number is Positive, Negative, or Zero
  • Display Fibonacci Sequence Using Recursion

JavaScript Tutorials

  • JavaScript Math sqrt()
  • JavaScript Number toFixed()
  • JavaScript break Statement
  • JavaScript while and do...while Loop
  • JavaScript if...else Statement
  • JavaScript continue Statement

Javascript Program to Solve Quadratic Equation

To understand this example, you should have the knowledge of the following JavaScript programming topics:

This program computes roots of a quadratic equation when its coefficients are known.

The standard form of a quadratic equation is:

To find the roots of such equation, we use the formula,

The term b 2 -4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots.

  • If the discriminant is greater than 0 , the roots are real and different .
  • If the discriminant is equal to 0 , the roots are real and equal .
  • If the discriminant is less than 0 , the roots are complex and different .

Nature of the roots of quadratic equations

Example: Roots of a Quadratic Equation

The above input values satisfy the first if condition. Here, the discriminant will be greater than 0 and the corresponding code is executed.

The above input values satisfy the  else if condition. Here, the discriminant will be equal to 0 and the corresponding code is executed.

In the above output, the discriminant will be less than 0 and the corresponding code is executed.

In the above program, the Math.sqrt() method is used to find the square root of a number. You can see that toFixed(2) is also used in the program. This rounds up the decimal number to two decimal values.

The above program uses an  if...else statements. If you want to learn more about if...else statements, go to JavaScript if...else Statement .

Sorry about that.

Solver Title

Practice

Generating PDF...

  • Pre Algebra Order of Operations Factors & Primes Fractions Long Arithmetic Decimals Exponents & Radicals Ratios & Proportions Percent Modulo Number Line Mean, Median & Mode
  • Algebra Equations Inequalities System of Equations System of Inequalities Basic Operations Algebraic Properties Partial Fractions Polynomials Rational Expressions Sequences Power Sums Interval Notation Pi (Product) Notation Induction Logical Sets Word Problems
  • Pre Calculus Equations Inequalities Scientific Calculator Scientific Notation Arithmetics Complex Numbers Polar/Cartesian Simultaneous Equations System of Inequalities Polynomials Rationales Functions Arithmetic & Comp. Coordinate Geometry Plane Geometry Solid Geometry Conic Sections Trigonometry
  • Calculus Derivatives Derivative Applications Limits Integrals Integral Applications Integral Approximation Series ODE Multivariable Calculus Laplace Transform Taylor/Maclaurin Series Fourier Series Fourier Transform
  • Functions Line Equations Functions Arithmetic & Comp. Conic Sections Transformation
  • Linear Algebra Matrices Vectors
  • Trigonometry Identities Proving Identities Trig Equations Trig Inequalities Evaluate Functions Simplify
  • Statistics Mean Geometric Mean Quadratic Mean Average Median Mode Order Minimum Maximum Probability Mid-Range Range Standard Deviation Variance Lower Quartile Upper Quartile Interquartile Range Midhinge Standard Normal Distribution
  • Physics Mechanics
  • Chemistry Chemical Reactions Chemical Properties
  • Finance Simple Interest Compound Interest Present Value Future Value
  • Economics Point of Diminishing Return
  • Conversions Radical to Exponent Exponent to Radical To Fraction To Decimal To Mixed Number To Improper Fraction Radians to Degrees Degrees to Radians Hexadecimal Scientific Notation Distance Weight Time
  • Pre Algebra
  • One-Step Addition
  • One-Step Subtraction
  • One-Step Multiplication
  • One-Step Division
  • One-Step Decimals
  • Two-Step Integers
  • Two-Step Add/Subtract
  • Two-Step Multiply/Divide
  • Two-Step Fractions
  • Two-Step Decimals
  • Multi-Step Integers
  • Multi-Step with Parentheses
  • Multi-Step Rational
  • Multi-Step Fractions
  • Multi-Step Decimals
  • Solve by Factoring
  • Completing the Square
  • Quadratic Formula
  • Biquadratic
  • Logarithmic
  • Exponential
  • Rational Roots
  • Floor/Ceiling
  • Equation Given Roots
  • Newton Raphson
  • Substitution
  • Elimination
  • Cramer's Rule
  • Gaussian Elimination
  • System of Inequalities
  • Perfect Squares
  • Difference of Squares
  • Difference of Cubes
  • Sum of Cubes
  • Polynomials
  • Distributive Property
  • FOIL method
  • Perfect Cubes
  • Binomial Expansion
  • Negative Rule
  • Product Rule
  • Quotient Rule
  • Expand Power Rule
  • Fraction Exponent
  • Exponent Rules
  • Exponential Form
  • Logarithmic Form
  • Absolute Value
  • Rational Number
  • Powers of i
  • Partial Fractions
  • Is Polynomial
  • Leading Coefficient
  • Leading Term
  • Standard Form
  • Complete the Square
  • Synthetic Division
  • Linear Factors
  • Rationalize Denominator
  • Rationalize Numerator
  • Identify Type
  • Convergence
  • Interval Notation
  • Pi (Product) Notation
  • Boolean Algebra
  • Truth Table
  • Mutual Exclusive
  • Cardinality
  • Caretesian Product
  • Age Problems
  • Distance Problems
  • Cost Problems
  • Investment Problems
  • Number Problems
  • Percent Problems
  • Addition/Subtraction
  • Multiplication/Division
  • Dice Problems
  • Coin Problems
  • Card Problems
  • Pre Calculus
  • Linear Algebra
  • Trigonometry
  • Conversions

Click to reveal more operations

Most Used Actions

Number line.

  • roots\:-6x^{2}+36x-59
  • roots\:x^{2}-x-6
  • roots\:x^{2}-1
  • roots\:x^{2}+2x+1
  • roots\:2x^{2}+4x-6
  • How do you find the root?
  • To find the roots factor the function, set each facotor to zero, and solve. The solutions are the roots of the function.
  • What is a root function?
  • A root is a value for which the function equals zero. The roots are the points where the function intercept with the x-axis
  • What are complex roots?
  • Complex roots are the imaginary roots of a function.
  • How do you find complex roots?
  • To find the complex roots of a quadratic equation use the formula: x = (-b±i√(4ac – b2))/2a

roots-calculator

  • Middle School Math Solutions – Equation Calculator Welcome to our new "Getting Started" math solutions series. Over the next few weeks, we'll be showing how Symbolab... Read More

Please add a message.

Message received. Thanks for the feedback.

  • 90% Refund @Courses
  • Java Arrays
  • Java Strings
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Spring Boot

Related Articles

  • Solve Coding Problems
  • Java Program to Represent Linear Equations in Matrix Form
  • Java Program to Convert Byte Array to Image
  • Generate Random Numbers Using Middle Square Method in Java
  • How to Add Hyperlink to the Contents of a Cell using Java?
  • Java Program to Apply Delaunay Triangulation Algorithm
  • Java Program to Find the Area of Rhombus
  • How to Format the Text in a Word Document using Java?
  • Java Program to Find the Volume and Surface Area of Cuboid
  • Java Program to Illustrate a Method with 2 Parameters and without Return Type
  • Java Program to Get CPU Serial Number for Windows Machine
  • Java Program to Find the Maximum Element in a Matrix
  • Java Program to Convert Double to Long
  • Java Program to Generate Random Hexadecimal Bytes
  • Java Program to Implement Inversion Method for Random Number Generation
  • How to Implement Stack in Java Using Array and Generics?
  • Java Program to Convert Int to Double
  • Java Program for Getting System UUID for Linux Machine
  • Java Program to Find Sum of N Numbers Using Recursion
  • Program to Emulate N Dice Roller

Java Program to Find the Roots of Quadratic Equation

The roots of a function are the x-intercepts. By definition, the y-coordinate of points lying on the x-axis is zero . Therefore, to find the roots of a quadratic function, we set f (x) = 0, and solve the equation, ax 2 + bx + c = 0.

Conditions for a quadratic equation – 

A mathematical formula for finding the roots of a quadratic equation – 

The roots of the quadratic equations are – 

The (b^2 – 4ac) which is the determinant, tells us about the nature of the roots –

  • if (b^2 – 4ac) > 0 , roots are real and different
  • if (b^2 – 4ac) == 0 , roots are real and equal
  • if (b^2 – 4ac) < 0 , roots are complex and different

Code to find roots of a quadratic equation:

Time Complexity: O(log(D)), where D is the discriminant of the given quadratic equation. Auxiliary Space: O(1)

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Please Login to comment...

author

  • pushpeshrajdx01
  • Top 12 AI Testing Tools for Test Automation in 2024
  • 7 Best ChatGPT Plugins for Converting PDF to Editable Formats
  • Microsoft is bringing Linux's Sudo command to Windows 11
  • 10 Best AI Voice Cloning Tools to be Used in 2024 [Free + Paid]
  • 10 Best IPTV Service Provider Subscriptions

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. C++ Program to Find Quadratic Equation Roots

    write program to find roots of quadratic equation

  2. C Program to find Roots of a Quadratic Equation

    write program to find roots of quadratic equation

  3. C++ Program To Find The Roots Of Quadratic Equation

    write program to find roots of quadratic equation

  4. Find Roots of Quadratic Equation: C Program

    write program to find roots of quadratic equation

  5. C Program to Find All Roots of Quadratic Equation

    write program to find roots of quadratic equation

  6. Program to find the roots of a quadratic equation in Python

    write program to find roots of quadratic equation

VIDEO

  1. Quadratic Equations||10th class maths||Find roots of a quadratic equation||CBSE & Matric||Part 13

  2. Quadratic Equations||10th class maths||Find roots of a quadratic equation||CBSE & Matric||Part 14

  3. C Program To Find Roots Of A Quadratic Equation

  4. Program to find roots of quadratic equation in C language

  5. C Program to find the roots of Quadratic Equations || Tamil

  6. Basic Math Review: Finding the Roots of Quadratic Equation using Quadratic Formula

COMMENTS

  1. C Program to Find the Roots of a Quadratic Equation

    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. It tells the nature of the roots. If the discriminant is greater than 0, the roots are real and different.

  2. Program to find the Roots of a Quadratic Equation

    Courses Practice Video Given a quadratic equation in the form ax2 + bx + c, (Only the values of a, b and c are provided) the task is to find the roots of the equation. Examples: Input: a = 1, b = -2, c = 1 Output: Roots are real and same 1 Input : a = 1, b = 7, c = 12 Output: Roots are real and different -3, -4 Input : a = 1, b = 1, c = 1

  3. Python Program to Solve Quadratic Equation

    Enter a: 1 Enter b: 5 Enter c: 6 The solutions are (-3+0j) and (-2+0j) We have imported the cmath module to perform complex square root. First, we calculate the discriminant and then find the two solutions of the quadratic equation. You can change the value of a, b and c in the above program and test this program.

  4. C++ Program to Find All Roots of a Quadratic Equation

    For a quadratic equation ax2+bx+c = 0 (where a, b and c are coefficients), it's roots is given by following the formula. Formula to Find Roots of Quadratic Equation The term b 2 -4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.

  5. C Program for Quadratic Equation Roots

    Program to Find Roots of a Quadratic Equation C #include <math.h> #include <stdio.h> #include <stdlib.h> void findRoots (int a, int b, int c) { if (a == 0) { printf("Invalid"); return; } int d = b * b - 4 * a * c; double sqrt_val = sqrt(abs(d)); if (d > 0) { printf("Roots are real and different\n");

  6. Python Program to find roots of a Quadratic Equation

    The mathematical representation of a Quadratic Equation is ax²+bx+c = 0. A Quadratic Equation can have two roots, and they depend entirely upon the discriminant. If discriminant > 0, then Two Distinct Real Roots exist for this equation If discriminant = 0, Two Equal and Real Roots exist. And if discriminant < 0, Two Distinct Complex Roots exist.

  7. C program to find all roots of a quadratic equation

    Articles C program to find all roots of a quadratic equation Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Logic to find roots of quadratic equation in C programming. Input a: 8 Input b: -4 Input c: -2 Output Root1: 0.80 Root2: -0.30

  8. C Program to find Roots of a Quadratic Equation

    How to write a C Program to find the Roots of a Quadratic Equation with an example? A Quadratic Equation in C can have two roots, and they depend entirely upon the discriminant. The mathematical representation of a Quadratic Equation is ax²+bx+c = 0. If discriminant > 0, then Two Distinct Real Roots will exist for this equation

  9. C Program to find the Roots of a Quadratic Equation

    Step by step let's understand how we can write a program that can be used to find the roots of a quadratic equation. First, we will ask the user for the values of a, b, and c to form the quadratic equation. Once you have the values, you need to check if the value of a, entered is not 0. If it's 0 our x2 term will be 0 and our equation won ...

  10. C program to find all roots of a quadratic equation using switch case

    Root1: 1.85 Root2: -1.35 Required knowledge Basic C programming, Relational operators, Switch case statement Learn more - Program to find roots of quadratic equation using if…else. Quadratic equation In elementary algebra quadratic equation is an equation in the form of Solving quadratic equation

  11. C Program to Find the Roots of Quadratic Equation

    A quadratic equation is an equation of the form ax2 + bx + c = 0 where a, b and c are constants. And the formula to calculate the roots of the quadratic equation is: C program to find the roots of a quadratic equation is shown below. The values a, b and c are entered by the user. Then the value of x (i.e. the value of b 2 -4ac) is calculated.

  12. Java Program to Find all Roots of a Quadratic Equation

    Java if...else Statement Java Math sqrt () The standard form of a quadratic equation is: ax2 + bx + c = 0 Here, a, b, and c are real numbers and a can't be equal to 0. We can calculate the root of a quadratic by using the formula: x = (-b ± √ (b2-4ac)) / (2a) The ± sign indicates that there will be two roots:

  13. python

    Below is the Program to Solve Quadratic Equation. For Example: Solve x2 + 3x - 4 = 0. This quadratic happens to factor: x2 + 3x - 4 = (x + 4) (x - 1) = 0. we already know that the solutions are x = -4 and x = 1. # import complex math module import cmath a = 1 b = 5 c = 6 # To take coefficient input from the users # a = float (input ...

  14. C++ Program To Find The Roots Of Quadratic Equation

    Courses Practice Given a quadratic equation in the form ax2 + bx + c, find roots of it. Examples: Input : a = 1, b = -2, c = 1 Output: Roots are real and same 1 Input : a = 1, b = 7, c = 12 Output: Roots are real and different -3, -4 Input : a = 1, b = 1, c = 1 Output: Roots are complex -0.5 + i1.73205 -0.5 - i1.73205

  15. Python program to solve quadratic equation

    Using the direct formula Using the below quadratic formula we can find the root of the quadratic equation. The values of the roots depend on the term (b2 - 4ac) which is known as the discriminant (D). We have three cases of discriminant as given below: Case 1: D > 0 (b*b > 4*a*c) Roots are real and different

  16. Roots of Quadratic Equation

    The roots of a quadratic equation are the values of the variable that satisfy the equation. They are also known as the "solutions" or "zeros" of the quadratic equation.For example, the roots of the quadratic equation x 2 - 7x + 10 = 0 are x = 2 and x = 5 because they satisfy the equation. i.e., when each of them is substituted in the given equation we get 0.

  17. Quadratic Formula Calculator: Wolfram|Alpha

    A useful tool for finding the solutions to quadratic equations. Wolfram|Alpha can apply the quadratic formula to solve equations coercible into the form ax2 +bx+c= 0 a x 2 + b x + c = 0. In doing so, Wolfram|Alpha finds both the real and complex roots of these equations. It can also utilize other methods helpful to solving quadratic equations ...

  18. C Program to Find Roots of Quadratic Equation

    A quadratic has the standard form y = ax2 + bx + c, where a, b, and c are numbers and a cannot be zero. Example of a quadratic equation: 3x^2 + 3x + 1. Let's look at what quadratic equation roots are and how to get them from the equation. The term b^2 - 4ac is called the discriminant of a quadratic equation. It tells the nature of the roots.

  19. Roots of Quadratic Equation

    Roots of Quadratic Equations are also called Zeros of a Quadratic Equation or Solutions of a Quadratic Equation. Quadratic equations are mathematical expressions of the form ax 2 + bx + c = 0, where a, b, and c are constants, and x represents the variable.; Solving for the values of x that make this equation true yields the roots of the quadratic equation.

  20. Javascript Program to Solve Quadratic Equation

    To find the roots of such equation, we use the formula, (root1,root2) = (-b ± √b 2 -4ac)/2 The term b 2 -4ac is known as the discriminant of a quadratic equation. It tells the nature of the roots. If the discriminant is greater than 0, the roots are real and different. If the discriminant is equal to 0, the roots are real and equal.

  21. Roots Calculator

    To find the complex roots of a quadratic equation use the formula: x = (-b±i√(4ac - b2))/2a; Show more; roots-calculator. en. Related Symbolab blog posts. High School Math Solutions - Radical Equation Calculator. Radical equations are equations involving radicals of any order. We will show examples of square roots; higher...

  22. Quadratic Formula Calculator

    Calculator Use. This online calculator is a quadratic equation solver that will solve a second-order polynomial equation such as ax 2 + bx + c = 0 for x, where a ≠ 0, using the quadratic formula. The calculator solution will show work using the quadratic formula to solve the entered equation for real and complex roots.

  23. Java Program to Find the Roots of Quadratic Equation

    Practice The roots of a function are the x-intercepts. By definition, the y-coordinate of points lying on the x-axis is zero. Therefore, to find the roots of a quadratic function, we set f (x) = 0, and solve the equation, ax 2 + bx + c = 0. Conditions for a quadratic equation -