Last Updated on May 10, 2023 by Prepbytes
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.
Algorithm to Find the Roots of Quadratic Equations in C
- Input the value of a, b, c.
- Calculate k = bb – 4a*c
- If (d < 0)
- Print "Roots are Imaginary, calculate root1 = (-b +i ?k)/ 2a and root2 =(b + i?k)/ 2a.
- else if (d = 0)
- Print "Roots are Equal" and calculate root1 = root2 = (-b / 2*a)
- else
- Print "Roots are real and calculate root1 = -b + ?d / 2a and root2 = -b – ?d / 2a.
- Print root1 and root2.
- End the algorithm.
Code Implementation to Find Roots of Quadratic Equations in C
#include <math.h> #include <stdio.h> int main() { double a, b, c, discriminant, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: "); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); } else { realPart = -b / (2 * a); imagPart = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); } return 0; }
Output
root1 = -1.00+1.41i and root2 = -1.00-1.41i
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