The WP Debugging plugin must have a wp-config.php file that is writable by the filesystem.

C Program to Find Roots of Quadratic Equation

C Program to Find Roots of Quadratic Equation

This article will deeply discuss c program to find roots of quadratic equation. Finding roots of quadratic equation in c will help in building the logic which will enhance your coding career. Let’s discuss and understand what a quadratic equation is and we will see the
C program
to find roots of quadratic equation.
Firstly, let’s look and see what a quadratic equation is?

What is a quadratic equation in C?

A quadratic equation as an equation of degree 2, meaning that the highest exponent of this function is 2.
The standard form of a quadratic is y = ax^2 + bx + c, where a, b, and c are numbers and a can’t be 0. Example of quadratic equation: 3x^2 + 3x + 1.
Let’s discuss what roots of a quadratic equation are and how to get the roots 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 Roots of Quadratic Equation in C Language

  1. Input the value of a, b, c.
  2. Calculate k = bb – 4a*c
  3. If (d < 0)
    • Print "Roots are Imaginary, calculate root1 = (-b +i ?k)/ 2a and root2 =(b + i?k)/ 2a.
  4. else if (d = 0)
    • Print "Roots are Equal" and calculate root1 = root2 = (-b / 2*a)
  5. else
    • Print "Roots are real and calculate root1 = -b + ?d / 2a and root2 = -b – ?d / 2a.
  6. Print root1 and root2.
  7. End the algorithm.

Code Implementation to Find Roots of Quadratic Equation in C Language

#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 blog gives the best explanation to find the roots of the quadratic equation in c. Regularly solving problems like finding roots of the quadratic equation will boost your career in the programming field. Don’t stop here, try more logical questions like finding roots of the quadratic equation, which not only build your logics but also by practicing them your programming skills will also boosten up.

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

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