C program to find roots of a quadratic equation

Roots of a quadratic equation

In this C program, we will learn how to find the roots of a quadratic equation[ax2 + bx + c]. When we try to solve the quadratic equation we find the root of the equation. Mainly roots of the quadratic equation are represented by parabola in 3 different patterns like

  • No Real Roots
  • One Real Root
  • Two Real Roots

When we solve the equation we get 3 conditions mentioned above using this formula:-
X = [-b (+or-)[Squareroot(pow(b,2)-4ac)]]/2a

 

Python program to find roots of quadratic equation

Algorithm

  • Step 1:- Start.
  • Step 2:- Take user inputs for a,b and c.
  • Step 3:- Check if the value of a is not zero.
  • Step 4:- Calculate Functions value ie, b*b – a*a*c.
  • Step 5:- Find the square root of the function.
  • Step 6:-if the value is greater than zero print Two real roots and value of roots.
  • Step 7:- If the value is equal to zero Print one real root and print the value.
  • Step 8:- If both the condition are false print no real roots and print values.

C program to find the square root of a quadratic equation

#include <stdio.h>
#include <math.h>
int main()
{
    int a, b, c;
    int val, root;
    printf("enter value of a : ");
    scanf("%d",&a);
    printf("enter value of b : ");
    scanf("%d",&b);
    printf("enter value of c : ");
    scanf("%d",&c);
    if(a==0)
    {
        printf("a cannot be zero");
    }
    else
    {
        val = (b*b) - (4*a*c);
        root = sqrt(val);
        if(val>0)
        {
            printf("two real roots\n");
            printf("%d\n",(-b+root)/(2*a));
            printf("%d",(-b-root)/(2*a));
        }
        else if(val==0)
        {
            printf("One real root\n");
            printf("%d",-b/(2*a));
        }
        else
        {
            printf("No real root\n");
            printf("%d + i %d\n",-b/(2*a),root);
            printf("%d - i %d\n",-b/(2*a),root);
       }      
    }
    return 0;
}
Output:
Enter value of a :1
Enter value of b :-7
Enter value of c :12
Two Real Roots
4
3